Today I want to talk about looking at only what you want.
For example, when you type Get-Service you see three columns (three properties) displayed by default:
- Status
- Name
- DisplayName
But we know a service has more properties than that! So how can we change what columns PowerShell displays here?
That’s the focus of this post.
Whenever you want to know everything there is to know about an object pipe it to Get-Member (GM).
gsv | gm
Okay, so let’s just select the Service Name, Status and Machine Name. That way we can see which services belong to which computers.
You see… when we simply type Get-Service without any qualifications it returns Status, Name and DisplayName.
These are the properties. Columns are properties and rows are objects. Each row is a unique Service object.
So if we want to change the view so Service Name, Status and Machine Name show up we need to explicitly select which properties of these objects we want displayed.
We do this by piping Get-Service
to Select-Object
and using the -Property
parameter with our list of column headers:
gsv | select -property name, status, machinename
Select
is just a partial parameter for Select-Object
(sort of like an alias) so don’t let that trip you up.
Okay, so let’s see if the DHCP service is running on the two computers in my lab: dc16alx1 and ws11alx1.
gsv dhcp -computername dc16alx1, ws11alx1 | select -property name, status, machinename
Nice, and now I can ship this data as a CSV, as an XML file or even as a HTML page. Or if I want I can just pipe it as a text file.
For example, here’s how we could export all that jazz to a CSV file.
gsv dhcp -computername dc16alx1, ws11alx1 | select -property name, status, machinename | Export-Csv -Path 'c:\dhcp.csv'
In the next post we’ll look into formatting the output.