Select-Object and -ExcludeProperty

by Feb 26, 2018

Here is a line of code that often puzzles PowerShell users:

Get-Service | Select-Object -ExcludeProperty Name

When you use Select-Object, its -ExcludeProperty parameter does not seem to do anything. The truth is: -ExcludeProperty only works when you also use -Property. So this works:

Get-Service | Select-Object -ExcludeProperty Name -Property Status, DisplayName, Name

However, this is ridiculous: why would you specify properties with -Property only to exclude them again with -ExcludeProperty? This would be much easier:

Get-Service | Select-Object -Property Status, DisplayName

In practice, -ExcludeProperty makes sense only when you use wildcards:

 
PS> Get-CimInstance -ClassName Win32_BIOS | Select-Object -Property *BIOS* -ExcludeProperty *major*, *minor*
 
 
PrimaryBIOS         : True
BiosCharacteristics : {7, 9, 11, 12...}
BIOSVersion         : {DELL   - 1072009, 1.6.1, American Megatrends - 5000B}
SMBIOSBIOSVersion   : 1.6.1
SMBIOSPresent       : True
 

Are you an experienced professional PowerShell user? Then learning from default course work isn’t your thing. Consider learning the tricks of the trade from one another! Meet the most creative and sophisticated fellow PowerShellers, along with Microsoft PowerShell team members and PowerShell inventor Jeffrey Snover. Attend this years’ PowerShell Conference EU, taking place April 17-20 in Hanover, Germany, for the leading edge. 35 international top speakers, 80 sessions, and security workshops are waiting for you, including two exciting evening events. The conference is limited to 300 delegates. More details at www.psconf.eu.

Twitter This Tip! ReTweet this Tip!