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="Size";Expression={$_.Length}},`
@{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 
ceID,VolumeName,`
@{label="Size(MB)";Expression={$_.Size/1mb -as [int]}},`
@{label="Free(MB)";Expression={$_.FreeSpace/1MB -
as [int]}},`
@{label="PercentFree";Expression={
"{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":

@{label="PercentFree";Expression={"{0:P2}" 
-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 Petri.com, and a frequent speaker at technology conferences and user groups.

Featured

comments powered by Disqus

Subscribe on YouTube