Get-Command, aliased to gcm, is a command that you’ll use pretty much every day of your working life as a Windows systems administrator.
As I stated earlier, you can’t expect to memorize all the commands you need. Instead, if you’re armed with some general knowledge, you can leverage Get-Command to ferret out the best command to use in any given situation.
Let’s imagine that you need to discover service information on one of your servers. Simply combine Get-Command with asterisk (*) wildcards to get the job done:
Get-Command -Name *service*
Remember that the asterisk wildcard represents one or more unknown characters. You’ll notice that I use parameter names instead of passing the -Name value positionally like this:
Making my code explicit is infinitely preferable to making assumptions regarding the script runner’s knowledge of parameter names, aliases, and so on.
Remember what I told you concerning noun prefixes? Let’s get a list of all the Active Directory commands on my Windows Server 2012 R2 domain controller:
Get-Command -Noun AD*
If you’re wondering how I know that Get-Command has a -Noun parameter, good for you! That’s the kind of question you should be asking. I’ll answer that question shortly, as I cover how to use Get-Help and Get-Member. In the meantime, if you know the name of the module you want, you can enumerate all of its commands by supplying the -Module parameter:
Get-Command -Module ActiveDirectory
Often you’ll wonder which commands support a particular parameter. The following “one liner” example searches your system for all commands that have an -AsJob parameter:
Get-Command -ParameterName “AsJob”
One neat trick is to run Get-Command against an alias. The output tells you which command is mapped to that alias.
PS C:\> Get-Command dir CommandType Name ----------- ---- Alias dir -> Get-ChildItem
Finally, the following pipeline, which I nabbed from the Get-Command help, lists the type(s) of objects output by all available commands on your system:
Get-Command -Type Cmdlet | Where-Object OutputType | Format-List -Property Name, OutputType
Speaking of command help, let’s learn how to use Get-Help next.