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 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.

Featured

  • Basic Authentication Extended to 2H 2021 for Exchange Online Users

    Microsoft is now planning to disable Basic Authentication use with its Exchange Online service sometime in the "second half of 2021," according to a Friday announcement.

  • Microsoft Offers Endpoint Configuration Manager Advice for Keeping Remote Clients Patched

    Microsoft this week offered advice for organizations using Microsoft Endpoint Configuration Manager with remote Windows systems that need to get patched, and it also announced Update 2002.

  • Azure Edge Zones Hit Preview

    Azure Edge Zones, a new edge computing technology from Microsoft designed to enable new scenarios for developers and partners, emerged as a preview release this week.

  • Microsoft Shifts 2020 Events To Be Online Only

    Microsoft is shifting its big events this year to be online only, including Ignite 2020.

comments powered by Disqus

Office 365 Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.