From automating simple but time-consuming tasks, to carrying out advanced functions in Microsoft 365 that aren’t available in the graphical interface, PowerShell has near limitless potential. Having a better understanding of how PowerShell works opens up a number of possibilities for how it can be used. In this blog, we will investigate some of the useful documentation features built directly into PowerShell and how they can assist in better understanding the cmdlets and objects that you are working with.
If you’re just getting started with PowerShell, first start with the basics – what it is, what it’s used for, and why it’s a formidable tool for administrators in today’s IT landscape.
Cmdlets make up the core of how PowerShell is used. If you are not familiar with what they are, head over to our PowerShell Primer article to take a look. One commonly demonstrated Cmdlet is Get-Process. The Get-Process command gets the processes on a local or remote computer. Without parameters, this cmdlet gets all of the processes on the local computer. You can also specify a particular process by process name or process ID (PID) or pass a process object through the pipeline to this cmdlet.
PowerShell has an impressive way of helping users work more productively as well as assisting in remembering the larger number of Cmdlets contained within the system – Tab Complete. From a PowerShell console, simply start typing the command. After a few characters, you can press the tab key to have PowerShell complete the Cmdlet for you.
Tab complete is a lifesaver when it comes to working with PowerShell, especially when what has been typed into the console is ambiguous and the options need to be cycled through. You can either continue pressing tab to move through the list or in newer versions of PowerShell, you can press Ctrl + Space to bring up a full list of available commands.
Two of my favorite Cmdlets
Combining one’s knowledge with what’s available using PowerShell’s built-in documentation can prove to be a powerful asset. Two of my favorite Cmdlets are Get-Help and Get-Member. In this next section, I’ll break down what these Cmdlets do and how I use them to support my workflow.
The description for what the Get-Process cmdlet does was mentioned above. That information can be attained online or directly from the PowerShell console. Having the information directly available is one of the significant advantages of PowerShell compared to other scripting languages. Information about the cmdlets, the correct syntax, list of parameters, and even examples can all be reached without leaving the console or having to involve an outside resource. There are, of course, lots of great resources online with detailed examples and explanations – but those aren’t always readily available or accessible on a machine that may not have an internet connection or even more constraining, a machine with no graphical user interface (GUI).
Here is the example of what it looks like to the help documentation for the Get-Process cmdlet:
Note the first time you run Get-Help, you may be prompted to download updated help files. This requires an active internet connection and will take some time. This process pulls the latest help information down locally to your machine. If you are not able to run the update you can still view the help information – it just may not be the latest version. In many cases, this can still help you work through running the command, but it may not have any updated documentation.
Let’s break down this output.
The first two sections provide the name of the cmdlet and a synopsis of what the command does. A detailed description can be found just below the syntax. The syntax section explains how the cmdlet can be run. It shows the parameters that can be passed to the cmdlet, what type of objects they need to be, and whether or not a parameter is required. You can find more information on PowerShell syntax here and here.
In the remarks section, there are a few additional parameters you can pass to the Get-Help Get-Process command to view more information. Most notable is the -examples switch parameter. This provides a list of examples for how the command works. A few things for you to try out:
Get-Help Get-Process -examples
Get-Help Get-Process -detailed
This provides you with a more detailed version of the information shown above.
And you can even run Get-Help against itself to view all the ways that you can discover information about how PowerShell works. So, if you’re in the mood for some “light lunchtime reading” check out Get-Help Get-Help -full for some riveting information (ok maybe that’s just an engineer thing).
Get-Member is one of the other commands that I often use when working in PowerShell. PowerShell is an object-orientated language. This means that we can reference different parts or attributes of an object as we are working in the console or with a script. An object also contains different methods (or functions) that it can perform. The Get-Member command provides a view of what the object looks like. Try it yourself with the Get-Process command:
Get-Process | Get-Member
For those that aren’t aware, the vertical bar character in the middle is called a pipe. It’s located above the enter key.
When you are running Get-Member, you are looking at the members (or properties) of an object. In the example above, you are looking at the parts of the Process object (System.Diagnostics.Process). The output of the above command allows you to see what you can do with the process object, how you can interact with it, and other attributes. Some of the methods worth mentioning are start (which will start a system process) and close (which does the same thing as its name implies). The complete output is a bit long to include here, but feel free to run the above command and see for yourself.
By using a combination of Get-Help and Get-Member, you can get a better understanding of how cmdlets interact with each other and objects. In a future article, we’ll investigate starting to harness the capabilities of PowerShell and how it can help save you time and effort when it comes to managing an environment.
For more tips and tricks, subscribe to our blog.