PowerShell: The Basics – Part 2 – Cmdlets and Modules

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.

In this post we will take a brief look at cmdlets and modules.

Cmdlets

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

Help information

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.

Syntax

Cmdlets follow a specific syntax, to ensure consistency. Every cmdlet consists of a verb, a noun, and optionally a prefix to the noun.
<Verb>-<Prefix><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.

Aliases

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.

Pipeline

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

CmdletsPurposeAlias
Get-HelpView help information about a cmdlethelp
Update-HelpDownload the latest help information from the internet
Clear-Host Clear the console clear, cls
Get-CommandList available cmdletsgcm
Get-ModuleList available/installed modulesgmo
Set-LocationChange working directorycd, chdir, sl
Get-ChildItemList files and directoriesdir, gci, ls
New-ItemCreate files or other objectsni
Remove-ItemRemove files or other objectsdel, erase, rd, ri, rm, rmdir
Get-ContentRead content from a filecat, gc, type
Get-ServiceList all servicesgsv
Get-ProcessList all processesgps, ps
Select-ObjectFilter the output from the previous cmdlets in the pipelineselect
Get-AliasList alias of cmdletsgal
Measure-ObjectMeasure the count, average or sum of multiple objectsmeasure
Measure-CommandMeasures how long time your code takes to execute
Format-ListFormats output in a pipeline as a listfl
Format-TableFormats output in a pipeline as a tableft

Modules

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:

Import-Module C:\Users\Martin\Github\Modules\AppLockerAdmin\AppLockerAdmin.psd1

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.

Further reading

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:

One comment

Leave a Reply