
PowerShell commands, also known as cmdlets (pronounced “command-lets”), are the heart of any PowerShell script. In this post, we’ll break down how to use and understand these commands, with a focus on making your IT job easier and more efficient.
What are PowerShell cmdlets?
In PowerShell, the tasks you perform are driven by cmdlets, specialized .NET classes with a specific purpose. Cmdlets follow a Verb-Noun naming pattern, making them relatively easy to understand. For example, Get-Process retrieves the list of all running processes.
Get-Process
Cmdlets are designed to do one specific thing: Get cmdlets retrieve data, Set cmdlets modify data, New cmdlets create data, and so on.
Anatomy of a cmdlet
Let’s dissect a typical PowerShell cmdlet to better understand its components:
Get-Service -Name "wuauserv"
Get-Serviceis the cmdlet itself, in this case, used to retrieve status information about a service.-Nameis a parameter that specifies which service we’re interested in."wuauserv"is the argument for the-Nameparameter. It specifies that we want to get the status of the Windows Update service.
Understanding parameters and arguments
Cmdlets usually come with parameters that allow you to customize the cmdlet’s behavior. For instance, the Get-Service cmdlet has a -Name parameter that allows you to specify the name of the service you want to retrieve.
Arguments are values you provide for the parameters. In the example above, "wuauserv" is an argument for the -Name parameter.
Using the pipeline
PowerShell’s pipeline (|) lets you pass the output of one cmdlet as the input to another. This feature allows you to perform complex tasks with simple, one-liner commands. For example:
Get-Process | Where-Object { $_.CPU -gt 10 }
This command retrieves the list of all running processes and pipes it to the Where-Object cmdlet, which filters the list to include only processes using more than 10 units of CPU time.
Exploring cmdlets with Get-Command and Get-Help
If you want to know which cmdlets are available, use the Get-Command cmdlet:
Get-Command -Noun Process
This command will list all cmdlets that have Process as their noun. To understand what a particular cmdlet does, use the Get-Help cmdlet:
Get-Help Get-Process
Best practices
Here are some best practices for working with PowerShell cmdlets:
- Make use of
Get-Helpto understand what a cmdlet does and what parameters it accepts. - Use the pipeline (
|) to create efficient, one-liner scripts. - Always validate the impact of your command with
-WhatIfparameter, especially when it could make changes to your system.
Remove-Item -Path C:\temp\* -WhatIf
Conclusion
Understanding PowerShell cmdlets is the first step towards writing powerful scripts and automating tasks. By grasping the structure of cmdlets and how they work, you can effectively leverage PowerShell to manage and maintain your IT infrastructure. Stay tuned for our next post where we will dive into the usage of Get-Help cmdlet to enhance your PowerShell knowledge.
More resources
- Cmdlet Overview | learn.microsoft.com
- PowerShell Language Specification 3.0: 13. Cmdlets | learn.microsoft.com
- Approved Verbs for PowerShell Commands | learn.microsoft.com
