
close
close
When I run a PowerShell training class, we naturally get around to talking about the Select-Object cmdlet. This is one of those all-purpose cmdlets that can be used in so many ways. Unfortunately, one of the ways often gives students more than a fair amount of trouble, so let’s see if I can’t demystify the process.
Let’s start with a basic PowerShell expression.
Get-CimInstance win32_logicaldisk -filter "deviceID='c:'"
Displaying disk usage (Image Credit: Jeff Hicks)
advertisment
Get-CimInstance win32_logicaldisk -filter "deviceID='c:'" | Select -Property DeviceID,Size,Freespace
Selecting key properties (Image Credit: Jeff Hicks)
Using PowerShell to convert to GB (Image Credit: Jeff Hicks)
Converting a value to an integer (Image Credit: Jeff Hicks)
Converting the freespace value (Image Credit: Jeff Hicks)
Get-CimInstance win32_logicaldisk -filter "deviceID='c:'" | Select -Property DeviceID,@{Name = "SizeGB"
The second element in the hashtable is the Expression. In a script or the PowerShell ISE, you could enter this on a new line:
@{Name = "SizeGB" Expression =
But in this situation, it is just as easy to insert the end of line marker, the semi-colon.
Get-CimInstance win32_logicaldisk -filter "deviceID='c:'" | Select -Property DeviceID,@{Name = "SizeGB" ; Expression =
What is the value? The value will be the result of a scriptblock.
advertisment
Get-CimInstance win32_logicaldisk -filter "deviceID='c:'" | Select -Property DeviceID,@{Name = "SizeGB" ; Expression = { } }
I also added the closing curly brace to hashtable.
A scriptblock is block of commands that PowerShell will execute. Whatever is written to the pipeline will be the value assigned to Expression. For the size, I’ve already figured out and tested the command. But I can’t simply paste in the actual value because that could change. Instead I can reference the property of the current object that is being processed using $_.
Get-CimInstance win32_logicaldisk -filter "deviceID='c:'" | Select -Property DeviceID,@{Name = "SizeGB" ; Expression = { $_.Size/1GB –as [int] } }
The expression scriptblock will take the size property of the incoming object, divide it by 1GB, and treat it as an integer. If I were to pipe multiple objects from a list of servers, then the expression would be applied to each one. Let’s try just this much.
Testing the custom hashtable property (Image Credit: Jeff Hicks)
Get-CimInstance win32_logicaldisk -filter "deviceID='c:'" | Select -Property DeviceID,@{Name = "SizeGB" ; Expression = { $_.Size/1GB –as [int] } }, @{Name = "FreeGB" ; Expression = { [math]::Round($_.Freespace/1gb,4)}}
All I had to do was plug my command into the expression scriptblock and reference to correct property name.
Testing new property hashtables (Image Credit: Jeff Hicks)
Get-CimInstance win32_logicaldisk -filter "deviceID='c:'" -computername $Computer | Select -Property DeviceID,@{Name = "SizeGB" ; Expression = { $_.Size/1GB –as [int] } }, @{Name = "FreeGB" ; Expression = { [math]::Round($_.Freespace/1gb,4)}}, @{Name = "PctFree" ; Expression = { [math]::Round(($_.freespace/$_.size)*100,2)}}, @{Name = "OS" ; Expression = { (Get-CimInstance Win32_Operatingsystem -ComputerName $_.pscomputername).caption}}, @{Name = "AuditDate" ; Expression = { Get-Date }}, @{Name = "Computername" ; Expression = {$_.PSComputername}}
I modified the command to query a remote computer. Next, I created an entirely new property called OS that runs another Get-CimInstance command to get the operating system. Noticed how I passed it the computername value from the PSComputername property of the current object.
Customized output (Image Credit: Jeff Hicks)
advertisment
More from Jeff Hicks
advertisment
Petri Newsletters
Whether it’s Security or Cloud Computing, we have the know-how for you. Sign up for our newsletters here.
advertisment
More in PowerShell
Microsoft’s New PowerShell Crescendo Tool Facilitates Native Command-Line Wraps
Mar 21, 2022 | Rabia Noureen
Most popular on petri
Log in to save content to your profile.
Article saved!
Access saved content from your profile page. View Saved
Join The Conversation
Create a free account today to participate in forum conversations, comment on posts and more.
Copyright ©2019 BWW Media Group