Windows Server How-To

How To Access Needed Info in PowerShell

Brien walks you through customizing a PowerShell cmdlet's output to get exactly the data you need.

This morning I was admittedly experiencing that problem that all writers have now and then in which I was having trouble deciding what to write about. I decided to go back through some of my previous columns to see if there were any topics that I had written about that I wanted to explore in greater depth. One such topic immediately jumped at me.

A few months ago, I wrote a column that talked about how to create reports in PowerShell. If you happened to read that post, then you know that PowerShell makes it relatively easy to export data to CSV files, text files and even HTML files. You can even use various HTML formatting tags to customize the look and feel of the reports that you create.

The post in which I discussed PowerShell reporting talks all about exporting PowerShell data into various file formats, but there was one thing that I wish I had included in that post. The post assumed that you knew how to use PowerShell to retrieve the data that you were interested in. In this column, I want to show you how to customize a PowerShell cmdlet's output so that any reports that you create based on that output contain exactly the data that you are interested in.

In PowerShell, the main command that is used to retrieve data for a particular object is Get. For example, you can use Get-VM to access virtual machine data, or Get-Disk to access disk data. Both of these cmdlets (and most other Get based cmdlets) can be used without including any additional parameters. Doing so usually yields a nice table including some basic data. For example, if you look at Figure 1, you can see that when I enter the Get-Disk cmdlet, PowerShell displays each disk's number, friendly name, operational status, total size, and partition style.

[Click on image for larger view.]  Figure 1. Entering the Get-Disk cmdlet provides basic information about all of the system's disks.

Obviously the information that is being returned is useful, and the information could easily be written to a CSV, text or an HTML file. The real question however, is what you would do if you needed a more granular report?

One thing that you can do to narrow things down is to tell PowerShell that you want to see information related to a specific disk. For example, if you only wanted to see information related to Disk 0, you could use a command like the one shown below:

Get-Disk –Number 0

If you look at Figure 2, you will notice that Number is actually the name of one of the column headers. PowerShell lets you filter by any column header. I might eventually write a blog post on filtering options. For right now though, there is something else that I want to show you.

[Click on image for larger view.]  Figure 2. You can tell PowerShell to display data for a specific disk.

When you enter a Get command, Windows will usually only display the most basic information that is available. There is typically a lot more data available than what is shown by default. If you want to see all of the available data, you must append the Select-Object cmdlet followed by an asterisks. To show you what I mean, check out Figure 3. In this screen capture, I have appended the Select-Object cmdlet to the command used in the previous figure as a way of displaying all of the available attributes for the selected disk.

[Click on image for larger view.]  Figure 3. PowerShell can display more data than what is shown by default.

Displaying all of the available data as I did in the previous screen capture is usually overkill. It is far more useful to display specific fields. If you look at the previous figure, you will notice that each piece of data has an attribute name. For example, the first line of output in the previous figure uses PartitionStyle as an attribute name. You can configure PowerShell to display specific attributes by listing the desired attributes after the Select-Object cmdlet. For example, you might use a command like this:

Get-Disk | Select-Object Number, BusType, HealthStatus,  Size

You can see what this command does in Figure 4.

[Click on image for larger view.]  Figure 4. You can control which attributes are displayed.

As you can see, it is possible to generate exactly the output that you want by using a combination of filtering and attribute selection. Once you create the desired output, that data can easily be written to a report file.

About the Author

Brien Posey is a seven time Microsoft MVP with over two decades of IT experience. As a freelance writer, Posey has written many thousands of articles and written or contributed to several dozen books on a wide variety of IT topics. Prior to going freelance, Posey was a CIO for a national chain of hospitals and healthcare facilities. He has also served as a network administrator for some of the country's largest insurance companies and for the Department of Defense at Fort Knox. When He isn't busy writing, Brien Posey enjoys exotic travel, scuba diving, and racing his Cigarette boat. You can visit his personal Web site at: www.brienposey.com.

comments powered by Disqus
Upcoming Events

Redmond Tech Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.