Finding a Command: Get-Command
Get-Command searches every command PowerShell knows, by its exact name, by its verb or noun, or by part of a name with the * wildcard, and shows what kind of command each one is and which module it comes from, without running it.
Apprentice · PowerShell · Updated
After this lesson you can
- Find commands by name, by verb or noun, or by part of a name with a wildcard, using Get-Command
- Read Get-Command's output to tell a cmdlet, a function or an alias, and the module it comes from
Key takeaways
Get-Commandlooks commands up without running them, so it's a safe way to check a guess.-Noun Servicelists everything you can do with services, and-Verb Restarteverything that restarts something.- The wildcard
*(read "star") stands for any letters, so*Clipboard*finds every command with Clipboard in its name. - The Source column names the module a command comes from, a package of related commands.
You want to copy a list from PowerShell straight to the clipboard, so you can paste it into an email. You guess there's a command with Clipboard in its name, but is it Copy-Clipboard, Set-Clipboard, Write-Clipboard? You could search the web and sort through forum posts. The answer is already on your computer: PowerShell keeps a list of every command it knows, thousands of them, and Get-Command searches it. A tech supporting a few hundred PCs uses it the same way, to find the command for a job they haven't done before.
Is there a command called...?
Give Get-Command a name, and it tells you whether that command exists:
Get-Command Get-Process
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Get-Process 7.0.0.0 Microsoft.PowerShell.Management
Each column is one fact:
- CommandType is what kind of command it is. A cmdlet is built into PowerShell, a function is a command written in PowerShell itself, and an alias is a short name for another command.
- Name is the command's name.
- Version and Source say where it comes from. Commands come in modules, packages of related commands, the way a toolbox holds the tools for one kind of job.
Microsoft.PowerShell.Managementcomes with PowerShell and holds the commands for processes, services, files and folders.
Get-Command only looks the command up. It never runs it, so it's the safe way to check a guess, even a guess like Stop-Process. A name that doesn't exist gets the same not recognized error as typing it at the prompt.
Every command for one noun, or one verb
Verb-noun names pay off here. -Noun Service lists every command that works on services:
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
Read down the list and you have everything PowerShell can do with a service. -Verb works the other way round, every command with one verb:
Get-Command -Verb Restart
CommandType Name Version Source
----------- ---- ------- ------
Function Restart-NetAdapter 2.0.0.0 NetAdapter
Function Restart-PcsvDevice 1.0.0.0 PcsvDevice
Function Restart-PrintJob 1.1 PrintManagement
Cmdlet Restart-Computer 7.0.0.0 Microsoft.PowerShell.Management
Cmdlet Restart-Service 7.0.0.0 Microsoft.PowerShell.Management
Cmdlet Restart-VM 2.0.0.0 Hyper-V
These come from several modules, some built into Windows rather than PowerShell, so your list depends on your computer: Restart-VM shows only where Windows' Hyper-V feature is turned on.
When you only know part of a name: `*`
Back to the clipboard. You know part of the name, not all of it. The wildcard * (read "star") stands for any letters, or none, so *Clipboard* means "anything, then Clipboard, then anything":
Get-Command *Clipboard*
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Get-Clipboard 7.0.0.0 Microsoft.PowerShell.Management
Cmdlet Set-Clipboard 7.0.0.0 Microsoft.PowerShell.Management
Set-Clipboard is the one that puts text on the clipboard. Get-Clipboard reads it back.
A plain wildcard search also finds programs on your computer, names that end in .exe, so a word as common as *Service* can bring back dozens of rows. A wildcard after -Noun or -Verb keeps the search to PowerShell's own commands. Get-Command -Noun *Printer* finds sixteen commands for printers, from Add-Printer to Write-PrinterNfcTag, most of them functions from the PrintManagement module.
On a Mac or Linux, the Windows modules aren't there, so -Noun Service and *Printer* find nothing; *Clipboard* finds the same two. In Windows PowerShell 5.1 the commands are the same, with older version numbers.
Try it. Find every command with the noun Date. Which of them only looks, and which changes something?
Check yourself
2 questions to try on the lesson page.
So what
When you're not sure what a command is called, you don't have to leave PowerShell to find out. Guess the verb or the noun, add a star where you're unsure, and Get-Command shows you what's there.