What’s the PowerShell command for…?” This happens to all of us. We’re working on a task and hit that speedbump where we either have forgotten or just don’t know the command to do that thing. This is where it’s very easy to turn to Google or Bing and and start searching. There is an easier way!
Really the issue with search engine results is that they may be incomplete, for a different version of PowerShell, or you just can’t find the answer. Whether it’s not using the right keywords, date constrictions, or whatever, the better way is right in front of you.
You have a PowerShell window open right? That’s why you are looking for commands right? Try this:
Get-Command
That’s nice, but on my machine ( with a few modules installed, granted) I get over 6,500 commands that way!
That’s not helpful without some limiting parameters. I do assume that you have an idea about what you are trying to accomplish. This is where the -noun and -verb parameters come in handy.
Let’s take a random example to walk through: What is the command to determine what services are running on a computer?
Our plain English request has verbs and nouns and PowerShell commands have verbs and nouns.
With a little knowledge of PowerShell structure, it’s pretty easy to work through.
Quick refresh: PowerShell commands (cmdlets) are structured in a “Verb-Noun” syntax.
Of all the verbs in the English language, there are (at this writing) 100 approved verbs. You can see the list at Approved Verbs for PowerShell Commands.
That’s a lot but we can narrow those down pretty quickly by remembering a few things – Common verbs are things like Add, Clear, Close, Copy, Enter, Exit, Find, Get, Hide etc. We recognize ‘Get’ from other things like GET-COMMAND
! ‘Find’ also looks attractive in this case, so let’s have a quick look at that.
PS> Get-command -verb find
CommandType Name Version Source
----------- ---- ------- ------
Function Find-Certificate 2.5.0.0 xCertificate
Function Find-Command 2.2.5 PowerShellGet
Function Find-Command 1.0.0.1 PowerShellGet
Function Find-DSCResource 2.2.5 PowerShellGet
Function Find-DscResource 1.0.0.1 PowerShellGet
Function Find-IpamFreeAddress 2.0.0.0 IpamServer
Function Find-IpamFreeRange 2.0.0.0 IpamServer
Function Find-IpamFreeSubnet 2.0.0.0 IpamServer
Function Find-Module 2.2.5 PowerShellGet
Function Find-Module 1.0.0.1 PowerShellGet
Function Find-NetIPsecRule 2.0.0.0 NetSecurity
Function Find-NetRoute 1.0.0.0 NetTCPIP
Function Find-RoleCapability 2.2.5 PowerShellGet
Function Find-RoleCapability 1.0.0.1 PowerShellGet
Function Find-Script 2.2.5 PowerShellGet
Function Find-Script 1.0.0.1 PowerShellGet
Cmdlet Find-Package 1.4.7 PackageManagement
Cmdlet Find-PackageProvider 1.4.7 PackageManagement
Ok that’s better but none of those seem to have anything to do with services. Let’s come at it from the ‘service’ side. You can list all the command with a certain noun or noun part using the -noun
parameter.
PS> get-command -noun service
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Get-Service 7.0.0.0 Microsoft.PowerShell.Management
Cmdlet New-Service 7.0.0.0 Microsoft.PowerShell.Management
Cmdlet Remove-Service 7.0.0.0 Microsoft.PowerShell.Management
Cmdlet Restart-Service 7.0.0.0 Microsoft.PowerShell.Management
Cmdlet Resume-Service 7.0.0.0 Microsoft.PowerShell.Management
Cmdlet Set-Service 7.0.0.0 Microsoft.PowerShell.Management
Cmdlet Start-Service 7.0.0.0 Microsoft.PowerShell.Management
Cmdlet Stop-Service 7.0.0.0 Microsoft.PowerShell.Management
Cmdlet Suspend-Service 7.0.0.0 Microsoft.PowerShell.Management
Aha! That first one Get-Service
looks good! Let’s try that one.
PS> Get-Service
Status Name DisplayName
------ ---- -----------
Running AarSvc_11d5ae Agent Activation Runtime_11d5ae
Running AdobeARMservice Adobe Acrobat Update Service
Running AdobeUpdateService AdobeUpdateService
Running AGMService Adobe Genuine Monitor Service
Running AGSService Adobe Genuine Software Integrity Serv…
Bingo! Of course I abbreviated that list. There are 299 services on this machine right now. Perhaps we should filter that down a bit. Parameters are a good way to do just that. You can find the parameters using the Get-Help
cmdlet.
PS> get-help get-service
NAME
Get-Service
SYNTAX
Get-Service [[-Name] <string[]>] [-DependentServices] [-RequiredServices]
[-Include <string[]>] [-Exclude <string[]>] [<CommonParameters>]
Get-Service -DisplayName <string[]> [-DependentServices]
[-RequiredServices] [-Include <string[]>] [-Exclude <string[]>]
[<CommonParameters>]
Get-Service [-DependentServices] [-RequiredServices] [-Include
<string[]>] [-Exclude <string[]>] [-InputObject <ServiceController[]>]
[<CommonParameters>]
ALIASES
gsv
Let’s say that the service we are interested in is something to do with printers. We can use wildcards in the -DisplayName
parameter to narrow this down
PS > Get-Service -DisplayName *print*
Status Name DisplayName
------ ---- -----------
Running DLPWD Dell Printer Status Watcher
Running DLSDB Dell Printer Status Database
Stopped PrintNotify Printer Extensions and Notifications
Stopped PrintWorkflowUser… PrintWorkflow_11d5ae
Running Spooler Print Spooler
Of course you can use the PowerShell pipeline to do Where-Object filtering, start/stop/restart the services discovered, but all of that is another story for another time!