Since you're ultimately only interested in the (transformed) property value, there's no need to use Select-Object
(whose alias is select
) at all - use ForEach-Object
instead:
Get-WmiObject Win32_OperatingSystem |
ForEach-Object { $_.ConvertToDateTime($_.LastBootUpTime) }
Note: The extra properties you saw, added by a remote Invoke-Command
call with a -ComputerName
argument (described below), are still technically present on the result, but they won't display.
That said, the WMI cmdlets were deprecated in PowerShell version 3. Using Get-CimInstance
in lieu of Get-WmiObject
actually makes the .ConvertToDateTime()
call unnecessary (the .LastBootUpTime
now directly contains a [datetime]
instance), in which case you can simply use Select-Object
's -ExpandProperty
parameter in order to return the property value only (rather than a [pscustomobject]
instance with the requested property):
Get-CimInstance CIM_OperatingSystem | Select-Object -ExpandProperty LastBootUpTime
Note: Get-CimInstance
directly supports a -ComputerName
argument, so you don't need Invoke-Command -ComputerName
for the invocation; unlike the firewall-unfriendly DCOM protocol that the WMI cmdlets use, the CIM cmdlets use the same, firewall-friendly transport as PowerShell remoting.
Or, more succinctly and efficiently, especially in a case such as this where the command returns only a single object, use direct property access:
(Get-CimInstance CIM_OperatingSystem).LastBootUpTime
This answer contrasts the pros and cons of these two approaches and shows other alternatives.
As for what you tried, which generally relates to:
Managing the origin properties automatically added by remote operations:
In remoting scenarios, PowerShell decorates the objects returned with additional properties that provide origin information. These properties are (mostly) of type NoteProperty
and are added:
when PowerShell remoting is involved - such as via Invoke-Command -ComputerName
in your case.
when CIM cmdlets such as Get-CimInstance
are directly used remotely, such as with the
-ComputerName
parameter.
These properties are:
.PSComputerName
(the name of the remote computer on which the code was executed)
- Note: On objects returned from remote CIM calls,
.PSComputerName
appears as a regular property (type Property
), not a NoteProperty
.
The associated hidden .PSShowComputerName
property, which defaults to $true
, which explains why you saw a PSComputerName
column in the display output.
- If you capture the objects before printing them to the screen, you can set the property to
$false
on them, in which case their .PSComputerName
property won't show (but will still be there) - however, the .RunspaceId
property may - situationally - still show, and would have to be explicitly excluded - see below.
PowerShell remoting only (not remote CIM calls): .RunspaceId
(the ID of the remote runspace)
To exclude these from local display / presence on the object, use the following techniques:
- If you're only interested in select properties, make the
Select-Object
call locally, which, by virtue of locally constructing new [pscustomobject]
instances with the properties of interest only, implicitly excludes the remoting-added properties:
Invoke-Command -ComputerName ... { ... } |
Select-Object Name, LastBootUpTime # LOCAL call to Select-Object
- If you're interested in all properties except the remoting-added ones, use
Select-Object -ExcludeProperty
to eliminate them explicitly:
# Get remote output, then locally exclude the remoting-added properties.
Invoke-Command -ComputerName ... { ... } |
Select-Object * -ExcludeProperty PSComputerName, PSShowComputerName, RunSpaceId
Note: Select-Object
generally returns [pscustomobject]
instances whose properties are static copies of the input objects and which lack the input type's methods.