PowerShell Essentials (Part 2)

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

Introduction

In the previous article in this series, I talked about the basic syntax used by a PowerShell cmdlet and I talked about Microsoft’s rationale for constructing cmdlets in this way. For those who might be wondering why I started out talking about something so basic, I did it because it fits within my primary goal for this article series.

My hope for this series is that I can help you to feel more confident about using PowerShell and get rid of PowerShell related anxiety. The reason why I started out by talking about the basic cmdlet syntax was to illustrate that even the most complicated PowerShell scripts are really nothing more than a long series of simple tasks.

Obviously, knowing the basic cmdlet syntax is just a starting point. There are some other things that you need to know in order to become proficient with PowerShell. That being the case, my plan is to compile a list of some of the most common questions about PowerShell and then answer those questions. Some of the questions that I want to address in this article series are:

  • How can I figure out which cmdlet to use?
  • How do I specify parameters for a cmdlet?
  • How can I get help with using a cmdlet?
  • How can I retrieve information about an object?
  • Why do some blocks of code not appear to be written in PowerShell?
  • How do PowerShell Scripts work?
  • Can I use variables?
  • How do PowerShell functions work?

By the time that I have finished answering these eight simple questions, you should have a solid working knowledge of PowerShell. I’m not saying that you will be an instant PowerShell expert, but by understanding the answers to the questions above, you should be able to accomplish just about anything necessary in PowerShell.

How Can I Figure Out Which Cmdlet to Use?

I want to get started by answering the first question on the list – how can I figure out which cmdlet to use? As previously explained, cmdlets are the basic building blocks of PowerShell. Each cmdlet performs a simple task and multiple cmdlets can be joined together to perform more complex tasks. Therefore, knowing which cmdlet or cmdlets to use in a given situation is the first step in completing the task at hand.

In spite of PowerShell’s reputation for being complicated, some cmdlets are actually very intuitive. That being the case, it is sometimes possible to guess the correct cmdlet. Don’t believe me? Let me prove it.

As I explained in the previous article, PowerShell cmdlets are made up of verb-noun combinations. The verb indicates the action to be performed, while the noun indicates the object to perform the action on. Even though PowerShell contains many thousands of cmdlets, the list of usable nouns and verbs isn’t as long as what you might expect. This means that sometimes it is possible to ask for what you need in plain English.

Suppose for instance that you wanted to use PowerShell to retrieve the current date. You could do so by using the Get-Date cmdlet. In this case, Get is the verb, and Date is the noun. A plain English cmdlet gave us exactly what we needed.

Admittedly, PowerShell wouldn’t have a reputation for being complicated if everything were that easy. Fortunately, there are a few other tricks that you can use to figure out what cmdlet to use.

One trick is to use a reasonable guess to figure out what you don’t know. Suppose for instance that you were working on a Windows 8 desktop and you wanted to see information related to the hard disk that is installed in the system. Your first thought might be to use the Get-Disk cmdlet. Get-Disk is a valid PowerShell cmdlet, but it is usually used with Windows Storage Spaces. That being the case, using the command locally on a Windows 8 desktop yields an error message. So what do you do?

Think about the task at hand. You are trying to retrieve disk related information, so the Get portion of the command is probably correct. One thing that you can do is to use the Get-Command command to determine all of the cmdlets that use the word Get. The command for doing so is Get-Command Get-* As you can see in Figure A however, the list of cmdlets that use Get is pretty long.

Image
Figure A: You can use Get-Command to retrieve a list of cmdlets.

Maybe it would be more helpful to get a list of disk related cmdlets. Since we are still trying to retrieve disk related information, we can include the Get verb in our query. So, if you wanted to see all of the cmdlets that use the Get verb and some variation of the word disk, you could use the following command:

Get-Command Get-*disk*

As you can see in Figure B, you get a much shorter list of results this time. Out of the list of results, the only cmdlet that really seems plausible is Get-PhysicalDisk. If you look at Figure C, you can see the error message produced by the Get-Disk cmdlet and the results returned by using the Get-PhysicalDisk cmdlet.

Image
Figure B: These are the only cmdlets used for retrieving disk information.

Image
Figure C: The Get-Disk cmdlet produces an error while Get-PhysicalDisk provides the information we wanted.

In my experience, it is usually possible to use the Get-Command cmdlet to locate the cmdlet that is required for a given situation. Every once in a while however, the required cmdlet is something really obscure that I never would have guessed. Fortunately, there are ways to get help in those situations.

Let’s pretend for example that I wanted to use PowerShell to delete a storage pool, but don’t know what cmdlet to use. My first guess would probably be to use the Delete-StoragePool cmdlet, but that guess would be wrong. The Delete-StoragePool cmdlet does not exist.

Next, I would probably use the trick that I showed you a moment ago and enter Get-Command Delete-*However, this produces no results. Clearly, Delete is not the right word.

There are a few different things that can be done in this situation. One option is to try to use the Get-Command cmdlet against the word StoragePool. The command for doing so is Get-Command *-StoragePool. In this case, PowerShell shows us four related cmdlets and it is easy to tell that the cmdlet that we are looking for is Remove-StoragePool. You can see the results in Figure D.

Image
Figure D: The command produced four different results.

OK, so that trick gave us the information that we needed, but what if it hadn’t? What else could we do? Well, one option is to use the Get-Help command. Since we know that we are trying to do something storage related, we could type Get-Help Storage. Doing so would provide us with a list of storage specific commands, as shown in Figure E.

Image
Figure E: The Get-Help Storage provides a list of storage related cmdlets.

Incidentally, the Get-Help cmdlet can also be used to get help with the cmdlet syntax. For instance, if you type Get-Help Remove-StoragePool, you will see the cmdlet’s full syntax, plus the contents of any available help files, as shown in Figure F.

Image
Figure F: You can use Get-Help to retrieve cmdlet syntax.

If the techniques that I have demonstrated do not work, then you can always use the Internet. You can for example perform an Internet search on the phrase “use PowerShell to remove a storage pool”.

Conclusion

As you can see, it is relatively easy to figure out what cmdlet to use in a given situation. In the next article in this series, I will continue the discussion by answering more questions from the list.

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

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top