Refactored - Top Rated Cloud Training

View Original

Running PowerShell Commands and Getting Help

*If you're just joining us, this post is part of a 12-part series on Getting Started with PowerShell. If you'd like to catch up (or skip ahead - more posts coming soon), click on the appropriate post below.
Getting Started with PowerShell
Windows PowerShell, PowerShell Core and PowerShell: Huh?
Installing PowerShell and Visual Studio Code

PowerShell is all about commands. Commands are the lifeblood of PowerShell. Commands allow you to read files, ping computers, start virtual machines, set registry keys and accomplish thousands of other tasks.

Cmdlets

The term 'command' is a generic term for any piece of code that runs or executes. In the old school command line (cmd.exe), for example, ping is a command while the server name to ping is not. The same principle applies to PowerShell. In PowerShell, a command can be a specific kind of PowerShell command called a cmdlet or a PowerShell function.

A PowerShell cmdlet is what you'll be using exclusively when you first begin your PowerShell journey. Cmdlets are all of the commands that come built into PowerShell itself. Common commands such as Get-Content, Get-Help, Get-Command and so on are all cmdlets.

Cmdlets are written in another language like C# and compiled to make them available for you to use.

A PowerShell function can also be considered a command but this topic is too advanced for beginners. You'll learn all about functions later in this blog series.

Verb-Noun Syntax

One of the best aspects of PowerShell for beginners is the cmdlets' verb-noun syntax. PowerShell differs from other shells like Bash by syntax verbosity. Bash, similar to other shells, has never focused on an intuitive command naming. You know immediately what a PowerShell command does simply by it's name.

All cmdlets are broken down in a verb-noun syntax. Every command performs some kind of action (verb) on some kind of object (noun). For example, if you need to get help for a command, you'd use the Get-Help command. If you need to find a running process, you'd use Get-Process. This syntax applies across all PowerShell cmdlets.

If you'd like to see all of the 'approved' verbs PowerShell uses, type Get-Verb.

Parameters

Many PowerShell commands have parameters. A parameter is a way to pass information to a command when it's run. A parameter is a way to change the behavior of a command.

Perhaps you'd like to ping a computer with the Test-Connection cmdlet. The Test-Connection cmdlet needs to know what computer to ping; it needs a parameter. You would specify the computer to ping by appending a dash followed by the parameter name followed by the parameter value.

To ping a computer called SRV1, you'd pass the value of SRV1 to the parameter called ComputerName on the Test-Connection cmdlet like below.

See this content in the original post

Discovering Commands

As soon as you open a PowerShell console, you'll be presented with a simple PS> prompt. That's it. So what now? What's actually possible to do? The Get-Command cmdlet is your best friend.

The Get-Command cmdlet discovers all of the commands currently available to you in your current session. Try it out. Type Get-Command and hit Enter. You'll soon be presented with a flurry of commands that quickly scroll off the screen.

Even though you could scroll up and down to see the available commands, you'll typically be looking for something specific. Let's narrow that result down a bit.

The Get-Command command has parameters that allow you to narrow down what's returned. For example, perhaps you need to find a command that reads some kind of object. Narrow down the results by using Get-Command -Verb Get. You'll see that only commands that start with 'get' are returned.

You can also find commands by their noun too like Get-Command -Noun Content. You'll soon see all of the cmdlets available that do something to text files.

If you scroll down a bit though, you'll probably notice a REMARKS section saying that it can't find any help files. This is common if you've never used the help system before.

PowerShell uses a feature called updatable help. Updatable help means that PowerShell can update help via the Internet. No more having to Google documentation for the latest version!

To ensure all of the cmdlets on your system have the latest help documentation, run the Update-Help cmdlet. When you do, you'll see a progress bar indicating all of the modules it's downloading new help content for.

Be sure you've opened up the PowerShell console as administrator before you run Update-Help

Once it's complete, let's now run Get-Help Get-Service -Examples. Using the Examples parameter displays many common usage examples as you can see below. Without running Update-Help, these examples wouldn't have been found.

Summary

It might be scary if you're opening up a PowerShell console for the first time. Don't worry! Start using the Get-Command and Get-Help cmdlets to see what's possible. Explore. Try new things. Break stuff! Get PowerShell to throw some errors at you. The most you experiment, the more you'll learn.