Prof. Powershell

Perfect Properties

PowerShell custom properties go beyond the call of duty by providing information as you need it.

Because PowerShell is an object-oriented shell, it offers a wealth of information. Pipe any object to Get-Member to discover its properties and methods. Once you know an object's properties, you can display them using Select-Object, even if you don't see them in the default view:

PS C:\> get-process | Select Name,ID,StartTime,Path

But you aren't limited to the property list you see with Get-Member. If you can create a PowerShell expression that will return a value for a given object, you can create a custom property for it.

Say you want to display a property called Uptime for a process object. There is pre-defined property. But we can calculate a value by subtracting the StartTime from the current time and returning a timespan object.
First we'll test the basic code with a single process:

PS C:\> (get-date)-(get-process taskmgr).starttime

Days              : 12
Hours             : 7
Minutes           : 41
Seconds           : 43
Milliseconds      : 883
Ticks             : 10645038835764
TotalDays         : 12.3206468006528
TotalHours        : 295.695523215667
TotalMinutes      : 17741.73139294
TotalSeconds      : 1064503.8835764
TotalMilliseconds : 1064503883.5764

We'll simplify by converting this to a string.

PS C:\> ((get-date)-(get-process taskmgr).starttime).ToString()
12.07:46:21.7045890

Now we need to do this for every process instead of just Task Manager. We'll use Select-Object and a hash table, also known as an associative array, to create a custom property and value. The format of the hash table is

@{name="propertyname";expression={powershell code}}

We will use $_ to represent the current object in the pipeline:

@{name="Uptime";Expression={((get-date)-$_.starttime).ToString()}}

This expression can be used like any other property name:

PS C:\> get-process | Select Name,ID,StartTime,@{name="Uptime";Expression={((get-date)-$_.starttime).ToString()}},Path

This also means we can use this new property in the pipeline like any other property:

PS C:\> get-process | where {$_.StartTime -and $_.name -ne "System"} | Sort StartTime -descending | Select Name,ID,,Path,StartTime,@{name="Uptime";Expression={((get-date)-$_.starttime).ToString()}}| Export-csv process-uptime.csv

This expression will export the standard Name, ID, Path and Starttime properties along with the custom Uptime and export the information to a CSV file.

By the way, I've inserted a Where-Object filter to remove any processes that don't have a start time, usually system-related, as well as the System process itself.

I use this technique all the time to provide extra and useful information. Next time, I'll show you a similar technique with the formatting cmdlets.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus

SharePoint Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.