Prof. Powershell

Perfect Properties

PowerShell custom properties provide 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 returns 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's pre-defined property. But we can calculate a value by subtracting the StartTime from the current time and returning a timespan object.

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

We'll do this for every process, not just Task Manager, using Select-Object and a hash table (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'll use $_ to represent the current object in the pipeline:

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

This expression can be used like any other property name:

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

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 -descend 
ing | Select 
Name,ID,,Path,StartTime,@{name="Uptime";Expres 
sion={((get-date)-$_.starttime).ToString()}}| 
Export-csv process-uptime.csv

This expression exports the standard Name, ID, Path and StartTime properties and the custom Uptime, and export the information to a .CSV file. 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.

About the Author

Jeffery Hicks is a multi-year Microsoft MVP in Windows PowerShell, Microsoft Certified Professional and an IT veteran with almost 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 works today as an independent author, trainer and consultant. Jeff is a regular contributor to a variety on online sites, as well as frequent speaker at technology conferences and user groups. Keep up with Jeff and his projects at http://jdhitsolutions.com/blog.

comments powered by Disqus
Upcoming Events

Redmond Tech Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.