This blog series will teach you the basics of PowerShell. You’ll learn common concepts such as variables, loops, functions and scripts in this series, along with PowerShell unique concepts such as cmdlets and the pipeline.
- PowerShell: The basics – Part 1 – Getting Started
- PowerShell: The Basics – Part 2 – Cmdlets and Modules
- PowerShell: The basics – Part 3 – Data Types, Variables, Operators, and Functions
- PowerShell: The basics – Part 4 – Strings, Arrays, Hashtables, Conditional Statements, and Loops
- PowerShell: The basics – Part 5 – Filtering and Formatting
- PowerShell: The basics – Part 6 – Remoting
In this post we will take a brief look at cmdlets and modules.
Specialized functions in PowerShell are called cmdlets, and you’ll find hundreds of these included by default in PowerShell. In part 1 you already used a cmdlet Clear-Host. A cmdlet takes zero or more parameters as arguments to modify the behavior. In many cases it’s also required to supply some or all parameters with values. An example of a cmdlet where we specify parameters to alter the behavior:
# Here we specify that we will list all text files located under C:\ Get-ChildItem -Path C:\ -Filter "*.txt" # View all available cmdlets Get-Command
To view helpful information about cmdlets you can use the cmdlet Get-Help in front of the cmdlets you want to show information about. Note that Get-Help supports multiple parameters to further tailor what help information you’ll see.
Parameters wrapped in square brackets in the help information are optional, while those that are not, are mandatory. Each cmdlet can support different sets of parameters supplied together, and you can only use one parameter set at a time.
Cmdlets follow a specific syntax, to ensure consistency. Every cmdlet consists of a verb, a noun, and optionally a prefix to the noun.
I.e. Get-ADUser, Get-Process, New-Item, Remove-ADUser
The verb signifies what you want to do, and the noun on what you will complete this action on. Custom modules usually include a prefix to the noun to differentiate them from existing modules and cmdlets.
Nouns should never be in the plural form, and verbs can only consist of approved verbs. A list of the approved verbs can be viewed by issuing Get-Verb. At this time of writing, there are 98 approved verbs.
For convenience many cmdlets also have a shorthand version, an alias. e.g. Clear-Host has the alias cls, which is much shorter to type. To work with aliases see the following:
# View the cmdlet an alias corresponds to Get-Alias cls # View the alias of a cmdlet (note that this cmdlet has multiple aliases) Get-Alias -Definition Clear-Host # Create your own alias for a cmdlet New-Alias -Name wipe -Value Clear-Host # View a list of all aliases in the session Get-Alias
Note: Best practice dictates to only use aliases when running ad-hoc commands, and should not be used in scripts and modules. This is to maintain readability of the code.
Most cmdlets generate output, usually the output are objects. Actually contrary to most other languages, nearly everything is an object in PowerShell. An object consists of properties, methods, events, and can even contain other objects.
You can pass the output from one cmdlets as input to another cmdlet. You do this over the pipeline. The pipeline is specified by the | character, and you pass input like this.
Get-Process | Select-Object -First 3 # or Get-Process | Select-Object -First 3 | Format-List
Some useful cmdlets
|Get-Help||View help information about a cmdlet||help|
|Update-Help||Download the latest help information from the internet|
|Clear-Host||Clear the console||clear, cls|
|Get-Command||List available cmdlets||gcm|
|Get-Module||List available/installed modules||gmo|
|Set-Location||Change working directory||cd, chdir, sl|
|Get-ChildItem||List files and directories||dir, gci, ls|
|New-Item||Create files or other objects||ni|
|Remove-Item||Remove files or other objects||del, erase, rd, ri, rm, rmdir|
|Get-Content||Read content from a file||cat, gc, type|
|Get-Service||List all services||gsv|
|Get-Process||List all processes||gps, ps|
|Select-Object||Filter the output from the previous cmdlets in the pipeline||select|
|Get-Alias||List alias of cmdlets||gal|
|Measure-Object||Measure the count, average or sum of multiple objects||measure|
|Measure-Command||Measures how long time your code takes to execute|
|Format-List||Formats output in a pipeline as a list||fl|
|Format-Table||Formats output in a pipeline as a table||ft|
While there are many cmdlets included in PowerShell by default, there are way more available for download, or bundled with other applications. Nowadays, most Microsoft systems comes with a unique bundle of cmdlets for administering that system. These bundles are what’s called modules, a collection of cmdlets.
You can view all modules that are loaded in the current session by issuing the cmdlet Get-Module. Note that not all installed modules are loaded by default. To view all installed modules, add the parameter -ListAvailable.
You can easily view what cmdlets are contained in a module by using the Get-Command cmdlet, and specifying the Module parameter.
You can import modules into the current session by using the cmdlet Import-Module, or by simply starting to use a cmdlet included in the module (the module will be automatically imported in the background as you write).
Certain folders are designated for storing PowerShell modules, and PowerShell will automatically find and load modules under these folders when they are needed. To view these folders issue the following command:
$env:PSModulePath -split ";"
Modules can also be directly imported by manually specifying the location:
Note that the file endings for PowerShell modules differs from script files (.ps1). File extensions for PowerShell modules are .psd1 (metadata abou the module), and psm1 (includes the actual code of the module).
We’ll take a close look at modules at a later time when we learn to create one ourselves.
In the next post we’ll take a look at data types, variables, operators, and functions. If you want more detailed reading on the topics of this post I suggest the following resources:
- Understanding important PowerShell Conecpets
- Getting detailed help information
- Understanding pipelines