Prof. Powershell

Label Me Perfect

The powers of properties allow you to use them as a PowerShell label maker.

I've previously explained how to add custom properties to an object with Select-Object, as well as use an associative array, sometimes known as a hash table, to create new properties.

The reason to do this with Select-Object is that the new property becomes part of the pipeline:

PS C:\files> dir -recurse | select FullName,LastWriteTime,`
@{name="Owner";Expression={(Get-ACL $_).Owner}}
 | Sort Owner,Size -desc | 
ConvertTo-HTML | out-file FileOwners.html

Here, I've used hash tables to rename the "Length" property to "Size," and created a new property called "Owner" that returns the file owner using the Get-ACL cmdlet. The resulting expression is then sorted by "Owner" and "Size" in descending order, converted to HTML and saved to a file.

If you only need to produce nicely formatted reports for the console or perhaps to save to a file, you could pipe the Select-Object expression to Format-Table. However, you can save a step and use hash tables with Format-Table to define the custom properties. The only change is that instead of defining a name, we're defining a label. Here's an example:

PS C:\> get-wmiobject Win32_logicaldisk -filter "dri 
vetype=3" `
-computer "mycompany-dc01" | Format-table Devi 
@{label="Size(MB)";Expression={$_.Size/1mb -as [int]}},`
@{label="Free(MB)";Expression={$_.FreeSpace/1MB -
as [int]}},`
"{0:P2}" -f (($_.FreeSpace/1MB)/($_.Size/1MB))}} -autosize

This takes output from a Get-WMIObject expression querying logical drives on computer MYCOMPANY-DC01 and creates a more user-friendly report. The values for "Size" and "FreeSpace" are in bytes.

Using a hash table, they've been reformatted to return values in megabytes. The label has also been modified to reflect the change:

@{label="Size(MB)";Expression={$_.Size/1mb -as [int]}}

The expression also creates a new value, "PercentFree," that's calculated from dividing the "FreeSpace" by "Size":

-f (($_.FreeSpace/1MB)/($_.Size/1MB))}}

When you run this expression, you should get output like this:

DeviceID VolumeName Size(MB) Free(MB) PercentFree
---- ----- ---- ---- ------
C:       OS_C	    8182	1410	17.24 %
E:       pagetemp	2046	1215	59.39 %
F:       Backups	4094	3141	76.72 %

You can also use custom labels with the Format-List cmdlet.

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.


  • Microsoft Endpoint Manager Improvements Highlighted at Ignite

    Improvements in the Microsoft Endpoint Manager (MEM) management solution were part of Tuesday's Microsoft Ignite online event.

  • Green City Illustration

    Microsoft Ignite 2020 Reaction, Part 1: A New Normal for Tech Conferences

    Something about Satya Nadella's opening keynote makes Brien wonder if Microsoft thinks we'd all be better off doing everything -- including conferences like Ignite -- remotely, even after the pandemic is over.

  • Microsoft Ignite: Azure Advances Across Five Frontiers

    To kick off the Microsoft Ignite virtual conference, CEO Satya Nadella made a bold claim about the public cloud with the second-largest market share behind Amazon.

  • Microsoft Buying Games Maker ZeniMax Media for $7.5 Billion

    Microsoft is buying ZeniMax Media, parent company of Bethesda Softworks and other game-maker affiliates, for $7.5 billion in cash.

comments powered by Disqus