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

  • Microsoft Previews New App Reporting and Consent Tools in Azure AD

    Microsoft last week described a few Azure Active Directory improvements for organizations wanting to connect their applications to Microsoft's identity and access service.

  • Free Software Foundation Asks Microsoft To Release Windows 7 Code

    The Free Software Foundation this week announced that it has established a petition demanding that Microsoft release its proprietary Windows 7 code as free software.

  • Managing Multiple Remote Connections in One Place with mRemoteNG

    If you're juggling multiple remote connections daily, this is the utility for you. Brien walks through the steps to use mRemoteNG, from installation to deployment.

  • Microsoft Unveils Plan To Push Bing to Office 365 ProPlus Users

    Microsoft on Tuesday unveiled plans to deliver an extension that will change the default search engine to Bing in both Google Chrome and Mozilla Firefox browsers for Office 365 ProPlus subscribers.

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.