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 

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 

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()

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:


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


This expression can be used like any other property name:

PS C:\> get-process | Select 

We can use this new property in the pipeline like any other property:

PS C:\> get-process | where {$_.StartTime -and 
$ -ne "System"} | Sort StartTime -descend 
ing | Select 
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 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, and a frequent speaker at technology conferences and user groups.


comments powered by Disqus

Hot Resources

Subscribe on YouTube