PowerShell Essentials (Part 3)

If you would like to read the other parts in this article series please go to:

Introduction

In my previous article, I listed eight questions with the goal of helping my readers to feel more comfortable with PowerShell by answering those eight questions. I answered the first question in the previous article, so now I want to move on to answering some of the other questions on the list.

How do I Specify Parameters for a PowerShell Cmdlet and How Can I Get Help with Using a Cmdlet?

Many PowerShell cmdlets can be used without the need for additional parameters (at least in certain circumstances). The Get-VM cmdlet for instance, returns a list of Hyper-V virtual machines without you needing to specify any additional parameters. Often times however, you will find that you need to specify one or more additional parameters in order to get a cmdlet to do exactly what you need for it to do. So how do you know which parameters you can use with a given cmdlet?

There are a number of different techniques that you can use, but my advice is to take advantage of the Get-Help cmdlet. To use this cmdlet, simply type Get-Help, followed by the cmdlet that you need assistance with.

To show you how this works, let’s pretend that you needed to retrieve a list of the physical disks that had been previously retired from your server. The Get-PhysicalDisk cmdlet is used to retrieve physical disk information, but how do you get information about retired disks?

The first step in the process would be to get help on the cmdlet. The actual command that you would enter is:

Get-Help Get-PhysicalDisk

When you enter this command, PowerShell will display the full syntax of the Get-PhysicalDisk cmdlet. It is common for this syntax information to be followed by usage examples, although Microsoft does not provide examples of how to use Get-PhysicalDisk. You can see what the syntax information looks like in Figure A.

Image
Figure A: Get-Help retrieves the full command syntax.

I will be the first to admit that the information that is displayed as a result of using the Get-Help cmdlet can be a little bit overwhelming. However, things really aren’t as difficult as they might at first appear. The trick is to know how to read the syntax information. To show you what I mean, let’s take a look at the last block of text in the screen capture above.

The text starts out with Get-PhysicalDisk. Obviously this is the cmdlet that we are using. From there, you will notice the following text:

[-Usage <usage[]>

Anything that appears in brackets [] references an optional parameter. In this case, we have a bracket [ followed by –Usage. This indicates that –Usage is an optional parameter.

The next thing that we see after –Usage is <usage[]> this indicates that the Usage parameter requires some additional information. You can’t just use the command Get-PhysicalDisk –Usage because even though Usage is a valid parameter, PowerShell needs more information. It needs to know what kind of usage you are interested in. Any time you see a word in between the less than < and greater than > signs, it indicates that you are going to need to provide some additional information to the parameter. So in this case, -Usage is the parameter and <usage[]> is the additional data.

Of course this raises the question of where we can find this additional data. Well, it is included in the command syntax. You will notice that the next section of the syntax is:

{Unknown | AutoSelect | ManualSelect | HotSpare | Retired | Journal}

You will notice that the list of words is enclosed in braces {}. These braces indicate that the words within can be used in conjunction to the optional parameter. To show you what I mean, let’s go back to the original example in which I wanted to find information related to retired physical disks. To get this information, I could use the following command:

Get-PhysicalDisk –Usage Retired

So you can see that I am appending the –Usage parameter to the Get-PhysicalDisk cmdlet, and then I am telling PowerShell what type of Usage I am interested in.

The syntax that is displayed within the above figure continues on, but the basic pattern repeats. The syntax information lists a number of optional parameters and the data that can be used with those parameters. Some of the other optional parameters for instance are –Description, -Manufacturer, -Model, -CanPool, and –HealthStatus, and the list goes on.

How Can I Retrieve Information About an Object?

At the very beginning of this article series, I explained that PowerShell cmdlets are based around verb-noun combinations. Nouns can be thought of as objects, and nearly every object has attributes. An attribute is data that describes the object. To show you what I mean, let’s go back and take a look once again at the Get-PhysicalDisk cmdlet, which you can see in Figure B.

Image
Figure B: Get-PhysicalDisk displays information about physical storage.

As you can see in the figure above, when I enter the Get-PhysicalDisk cmdlet, PowerShell displays each disk’s friendly name, whether or not the disk can be pooled, it’s operational status, its health status, its usage, and its size. Each of these fields represents an attribute of a physical disk.

There are two critically important things to know about this. First, knowing the names of the various fields (attributes) is key to being able to function in PowerShell. After all, how can you identify a specific disk from within a PowerShell script if you don’t know that the disk’s identity is tied to an attribute called FriendlyName.

The other thing to know is that typically the Get command does not display all of the available attributes. There are typically some additional attributes that are hidden from view. For example, PhysicalDisk has an attribute called Manufacturer, but that attribute is not displayed by the Get-PhysicalDisk cmdlet.

One of the most important PowerShell skills to master is retrieving the names of the available attributes. Fortunately, it’s really easy to do. You must simply append the Pipe symbol | and the Select-Object * cmdlet to the end of the cmdlet that you are using. For instance, if you wanted to see all of the available attributes for the Get-PhysicalDisk cmdlet, you would use the following command:

Get-PhysicalDisk | Select-Object *

You can see the output of this command in Figure C.

Image
Figure C: We are retrieving all of the PhysicalDisk attributes.

There are actually far more attributes than what is shown in the screen capture. It was impossible to fit all of the attributes on screen at once. The column on the left lists the attribute names.

In case you are wondering, the * after Select-Object tells PowerShell to display all of the attributes, but you can display select attributes instead. For example, suppose that I wanted to display the friendly name, serial number, spindle speed and physical sector size for each physical disk. I could do it with the following command:

Get-PhysicalDisk | Select-Object FriendlyName, SerialNumber, SpindleSpeed, PhysicalSectorSize

You can see the command’s output in Figure D. Remember that you aren’t limited to simply displaying attributes. PowerShell can make decisions based on attribute values. In some cases it is even possible to set attribute values yourself.

Image
Figure D: We can display a custom set of attributes.

Conclusion

In this article, I have shown you how to get help with PowerShell command syntax and how to retrieve attribute values. In the next article, I will answer some more questions from my list.

If you would like to read the other parts in this article series please go to:

Leave a Comment

Your email address will not be published.

Scroll to Top