Save to My DOJO
Table of contents
From the early days of PowerShell 1.0, released in 2006, PowerShell has revolutionized the way system administrators carry out automation tasks in Windows environments. Swiftly, it has replaced the beloved command shell in Windows as the preferred command-line tool of choice. PowerShell provides a relatively easy to learn, powerful, and flexible automation tool for many different use cases.
Part of the success of PowerShell has been a result of the PowerShell cmdlet. In this two-part series, we will take a detailed look at the PowerShell cmdlet. In part one, we will discuss what the PowerShell cmdlet is and how it can be used. In part two, we will take a closer look at how you write one, how they are installed, PowerShell vs. PowerShell Core, and installing PowerShell Core.
What are PowerShell cmdlets?
Let’s first take a look and see what a PowerShell cmdlet is precisely. A PowerShell cmdlet (pronounced commandlet) is a lightweight command used in PowerShell to perform an action. A PowerShell cmdlet is not like a DOS script that you may have written in the past. DOS scripts are known as uncompiled code. PowerShell cmdlets are like tiny executable programs that take advantage of all the constructs and built-in facilities found in .NET Framework.
Since it utilizes .NET framework, this means that PowerShell is object-oriented. It makes PowerShell much more robust than the standard command-line environments we have used traditionally. It can return more than simple strings. Instead, it can return objects and can be further used to perform other useful tasks. Since PowerShell works with objects, these objects have attributes and methods.
The robust framework of .NET makes writing a useful PowerShell cmdlet relatively easy to do. However, Windows 10 includes many useful PowerShell cmdlets already. When you access PowerShell for the first time, you can see many readily available cmdlets that can be used by default. The inclusion of this large number of cmdlets makes the “time to value” very quick, and it becomes immediately useful in most environments.
You can use the Get-Command to display the built-in cmdlets that are available. There are so many that you will need to use the Get-Command | more >parameter to scroll through the extensive list effectively.
Get-Command displaying a large number of built-in PowerShell cmdlets
With many built-in cmdlets available in Windows 10, you don’t have to know how to write a PowerShell cmdlet, or even know any .NET coding to begin working with PowerShell. You can start interacting with the built-in cmdlets and quickly using it to automate various processes and tasks.
Very human-readable
One of the strengths of using PowerShell as an IT administrator is it is very “human-readable.” Unlike other programming languages that may take years to become proficient, PowerShell is a very easy to learn language due to the way cmdlets follow an intuitive verb-noun pattern. PowerShell uses these intuitive verbs to perform operations in the environment. When you use the verb-noun pattern cmdlets such as Get-Help or Get-Process you are getting information returned to you.
When you use the verb-noun cmdlets such as Set-Disk, Set-DnsServer, and others, you set or modify the environment’s configuration. An action is performed. Again, these verb-noun patterns used with PowerShell are easy to understand and grasp for those new to the language. These are not the only verbs in PowerShell. Many others can be used, including Add, Convert, Copy, Debug, Disable, Export, and Move, to name a few.
Glossary of PowerShell Terms
To further understand PowerShell cmdlets, let’s define the following PowerShell terms to get a better picture of the underlying components.
- Classes – Classes specify details what PowerShell objects contain. The class is directly responsible for determining the actions that can be performed on an object
- Parameters – Parameters are extremely useful as they allow the PowerShell end-user to provide input to a PowerShell cmdlet
- Modules – Think of a module as a group of related PowerShell functionalities. This can be a set of assemblies, related resources, and script files that can be loaded
- Alias – An Alias is an alternate name that refers to a cmdlet or command. These can be user-defined
- Objects – PowerShell is based on .NET which is an object-oriented programming language. Everything contained in PowerShell is an object
- Methods – Methods are basically the instructions that define the actions that can be performed on PowerShell objects
Tab completion
There is a convenient way to enter the full names of the various PowerShell cmdlets available with PowerShell. Some of the cmdlets may contain several words in the verb-noun pattern. To help with the challenge of entering very long PowerShell cmdlets, you can use tab completion to take care of the process for you. In other words, you have to enter enough of the cmdlet to be unique from other cmdlets with similar names, hit Tab, and it will automatically complete the cmdlet name for you. When you press Tab continuously, it will cycle through the matching cmdlet names.
This process eliminates the tedious nature of typing out the full name of very long PowerShell cmdlets and saves time writing PowerShell code. Interestingly, tab completion is also effective with PowerShell cmdlet parameters. It means you can start typing a parameter for the cmdlet in question, and it will automatically complete the parameter name for you.
Built-in aliases and case
One of the great things Microsoft has built into PowerShell is aliases. Aliases in PowerShell provide access to the same functionality that many are familiar with in a DOS prompt. An example of how this is implemented in PowerShell is the dir command. As we all know, the dir command displays a directory listing of the files and folders in a particular directory.
Using the “dir” command in a command prompt
Using the dir command at a PowerShell prompt will return relatively the same information.
Running the dir command in PowerShell
Is PowerShell calling the dir command in a PowerShell environment? No, it is using the native Powershell Get-ChildItem cmdlet, aliased to the dir command. If you want to see this preconfigured alias, you can use the cmdlet Get-Alias.
Using Get-Alias to view aliases in PowerShell
With aliases in PowerShell, this often helps maintain and make use of a large vocabulary of DOS commands and other command-line tools without necessarily learning the PowerShell equivalent. However, one must keep in mind that a familiar command is an alias to a native PowerShell cmdlet. It means there are differences between the two in terms of parameters. You can’t use the same DOS parameters for the aliased cmdlets in PowerShell. Notice when attempting to use dir command parameters in PowerShell, you receive an error.
PowerShell aliases can’t use the same parameters as DOS command equivalents
What about uppercase vs. lowercase in PowerShell? You don’t have to worry with PowerShell about case. The PowerShell interpreter views the following as the same cmdlet:
- Get-Process, get-Process, get-process, Get-process
It helps alleviate any issues that you might see with PowerShell code based on upper and lower case cmdlets. Again, this reduces the barrier to entry when learning how to write PowerShell code.
Useful PowerShell cmdlets
There is a virtually unlimited number of useful PowerShell cmdlets. You can find a cmdlet or snippet of PowerShell code to do just about anything you want to do. Let’s list a few useful PowerShell cmdlets to note.
- Get-Help – This is one of the first basic cmdlets that you should become familiar with as it will help you understand what the different cmdlets in PowerShell can do. Get-Help will prompt you to update your help files online to ensure you have the latest version of help regarding the latest cmdlets. After you update, you can use Get-Help to get what is essentially a “man page” in the PowerShell world about any other cmdlet.
Using the Get-Help cmdlet to understand the Get-Service cmdlet
- Get-Service – The Get-Service cmdlet helps to quickly get a listing of your system’s various installed services. It includes their Status, Name, and DisplayName.
- Get-Process – Get-Process pulls a current list of running processes as well as their ProcessName and ID. You can easily pipe the returned process objects into the Stop-Process cmdlet to kill a running process.
- Restart-Computer – This cmdlet can restart both local and remote computers
- Shutdown-Computer – Use this cmdlet to shut down a local or remote computer
- Select-Object – The Select-Object cmdlet allows you to select a specific returned object from another PowerShell cmdlet. It will enable parsing down returned PowerShell objects to only those objects you need
- Stop-Process – Kill a specific running process on your system
- Get-ExecutionPolicy – You will need to understand the execution level of the system you are working with using PowerShell. It is a security mechanism to help define the various scripts that will be trusted to run.
- Set-ExecutionPolicy – Use the Set-ExecutionPolicy to configure the execution of PowerShell scripts on an end-user system.
- -WhatIf – The “WhatIf” parameter is not a cmdlet in its own right but rather a parameter that can provide the cmdlet action results before you run it. It is a handy parameter to use as it will detail the particular actions taken when running a specific cmdlet without actually running the cmdlet.
In this first part of the series, we have taken a detailed look at what a PowerShell cmdlet is, aliases, built-in cmdlets, useful PowerShell cmdlets, and how these are used. Stay tuned for the next part, where we will detail how you can go about writing your own PowerShell cmdlet, modules, PowerShell vs. PowerShell Core, installing PowerShell Core, and many other topics.
Not a DOJO Member yet?
Join thousands of other IT pros and receive a weekly roundup email with the latest content & updates!