Table of Contents
Selecting parts of objects
You can use the Select-Object cmdlet to create new, custom PowerShell objects that contain properties selected from the objects you use to create them.
Type the following command to create a new object that includes only the Name and FreeSpace properties of the Win32_LogicalDisk WMI class:
PS C:\> Get-CimInstance -Class Win32_LogicalDisk
DeviceID DriveType ProviderName VolumeName Size FreeSpace
-------- --------- ------------ ---------- ---- ---------
C: 3 OS 213970317312 79270346752
D: 3 DATA 297359372288 237870116864
P: 2 pCloud Drive 2199023255552 1442060361728
Z: 4 \\ExpanDrive\e8psvd7c-1 Amazon S3 10995116277760 10995116277760
PS C:\> Get-CimInstance -Class Win32_LogicalDisk | Select-Object -Property Name, FreeSpace
Name FreeSpace
---- ---------
C: 79271546880
D: 237870116864
P: 1442060361728
Z: 10995116277760
With Select-Object you can create calculated properties to display FreeSpace in gigabytes rather than bytes.
Get-CimInstance -Class Win32_LogicalDisk |
Select-Object -Property Name, @{
label='FreeSpace' ; expression={($_.FreeSpace/1GB).ToString('F2')}
}
Name FreeSpace
---- ---------
C: 73.83
D: 221.53
P: 1343.02
Z: 10240.00
Get-CimInstance -Class Win32_LogicalDisk |
Select-Object -Property Name, `
@{label='FreeSpace' ; expression={($_.FreeSpace/1GB).ToString('F2')}}, `
@{label='Volume' ; expression={$_.VolumeName}}
Name FreeSpace Volume
---- --------- ------
C: 73.84 OS
D: 221.53 DATA
P: 1343.02 pCloud Drive
Z: 10240.00 Amazon S3
| # | Learning PowerShell (Articles in series) | Cmdlets |
|---|---|---|
| #1 | Getting Started with Windows PowerShell | |
| #2 | Working with Windows PowerShell | |
| #3 | Writing and Running PowerShell Scripts | |
| #4 | Windows PowerShell Remoting | |
| #5 | Windows PowerShell Profiles | |
| #6 | PowerShell Drives and Providers | |
| #7 | Windows PowerShell ISE | |
| #8 | Manage Services using PowerShell | |
| #9 | Manage Processes using PowerShell | Get-Member, -Property, -ExpandProperty |
| #10 | Variables in Windows PowerShell | |
| #11 | Arrays in Windows PowerShell | |
| #12 | Hash Tables in Windows PowerShell | |
| #13 | PowerShell Loops | foreach, for |
| #14 | Conditional Logic in PowerShell | |
| #15 | PowerShell Functions | |
| #16 | One-liners and the Pipeline | |
| #17 | Everything about PSCustomObject | |
| #18 | Selecting Parts of Objects | |
| #19 | Sorting Objects | |
| #20 | Getting WMI objects with Get-CimInstance | |
| #21 | Parentheses, Braces and Square Brackets | () {} [] |
| #22 | The Philosophy Verb-Noun | Get-Command, Verb-Noun |
| #23 | PowerShell Parameters | $ |
| #24 | PowerShell and WMI | Get-CimInstance |



