Where to select

Using select-object (aliased to select in common usage) to reduce the number of properties passing into further processing is a common aspect of using PowerShell. However, its not the only place we can do it.

Consider this scenario.

We need to test the network client on a number of remote machines. We can use Win32_NetworkClient

PS > Get-WmiObject -Class Win32_NetworkClient

__GENUS          : 2
__CLASS          : Win32_NetworkClient
__SUPERCLASS     : CIM_LogicalElement
__DYNASTY        : CIM_ManagedSystemElement
__RELPATH        : Win32_NetworkClient.Name=”Microsoft Windows Network”
__PROPERTY_COUNT : 6
__DERIVATION     : {CIM_LogicalElement, CIM_ManagedSystemElement}
__SERVER         : RSLAPTOP01
__NAMESPACE      : root\cimv2
__PATH           : \\RSLAPTOP01\root\cimv2:Win32_NetworkClient.Name=”Microsoft Windows Network”
Caption          : Workstation
Description      : LanmanWorkstation
InstallDate      :
Manufacturer     : Microsoft Corporation
Name             : Microsoft Windows Network
Status           : OK

 

Now doing this on the local machine is fine but we might want to cut back on the properties returned across the network

PS > Get-WmiObject -Class Win32_NetworkClient -Property Name, Manufacturer, Caption, Description, Status

__GENUS          : 2
__CLASS          : Win32_NetworkClient
__SUPERCLASS     :
__DYNASTY        :
__RELPATH        : Win32_NetworkClient.Name=”Microsoft Windows Network”
__PROPERTY_COUNT : 5
__DERIVATION     : {}
__SERVER         :
__NAMESPACE      :
__PATH           :
Caption          : Workstation
Description      : LanmanWorkstation
Manufacturer     : Microsoft Corporation
Name             : Microsoft Windows Network
Status           : OK

This cuts down the data a bit – obviously for a class such as Win32_ComputerSystem that has a lot more properties you would get a bigger impact

Compare this to using select-object

PS > Get-WmiObject -Class Win32_NetworkClient |
select -Property Name, Manufacturer, Caption, Description, Status

Name         : Microsoft Windows Network
Manufacturer : Microsoft Corporation
Caption      : Workstation
Description  : LanmanWorkstation
Status       : OK

OK this looks better BUT remember for a remote machine the filtering happens AFTER the object has been returned.

Locally I would use select and remotely I would think about using –property on Get-WmiObject.

As so often it all depends on what you actually need to do

About these ads
This entry was posted in PowerShell and WMI, Powershell Basics. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s