Every now and again I see some people complaining about not getting the properties they want when using a PowerShell command.
For instance, someone was using the Get-Service command to query what was the “Startup Type” of WinRM service. For that the person used the following command:
As you can see, the “Startup Type” property that we can find on the user interface does not appear here!
"Wait wait wait...what? Is the command broken?"
In this case, this property does not belong to the default display properties set but the properties are still there!
So how can we get the list of available properties?
First, let me say that this person knows that
Select-Object can be used to select the properties we want, so he tried to guess the property name using a trial/error approach.
The person tried:
Get-Service WinRM | Select-Object Startup, Status, Name, DisplayName
Get-Service WinRM | Select-Object StartupType, Status, Name, DisplayName
But all of them were just empty.
Let me invoke a cliché but yet, still true: When I, and probably most of the people, started learning PowerShell, we learn that Get-Member (and also Get-Help) are our two best friends.
Get-Help needs no introduction, it will retrieve the help for a command! You should always start by reading the help of the command, you will find the overall description, parameters explained and even examples on how to execute the command.
Gets the properties and methods of objects.
This means it can give you a variety of information on the objects you are working with, including, for our use case, the available properties.
Let’s see if we can find the property we want. We can do this by piping the command we are working with to Get-Member.
Get-Service | Get-Member
We can see all the member types, but since we know we want to search on properties we can filter it down using:
Get-Service | Get-Member -MemberType Property
If it retrieves a big list we can also add a filter by the name we think it has like “Start”
Get-Service | Get-Member -MemberType Property -Name Start*
And, in this case we narrow it down to just one result -
StartType. Let’s try to include on our original command.
Get-Service WinRM | Select-Object StartType, Status, Name, DisplayName
Boom! We now have the property we are looking for!
I mentioned the
Select-Object * on the title of this post, that is because we can use it to get ALL existing properties that our object owns and their values.
Get-Service WinRM | Select-Object *
Why hide some properties by default?
This way it will become cleaner and faster. Faster: We can have 20 properties but if only 5 are the most useful, by setting this five the default the command will be faster than if we retrieve the whole 20 properties. Cleaner: We don’t fill the screen with information that 90% of the time is not useful for us.
Can we know beforehand what are the default properties of a command?
Yes, we can! And it is very easy actually.
Using our initial example:
There they are.
Getting the full list of properties:
If you use some properties a lot and they are not part of the defaults, or you just would like to change the default properties that are retrieved, you can use the
<a href="https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/update-typedata?view=powershell-6" rel="noopener" target="_blank">Update-TypeData</a> or
<a href="https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/update-formatdata?view=powershell-6" rel="noopener" target="_blank">Update-FormatData</a> cmdlets to make it work that way.
This post was intended to show / remember how you can know what are the default properties that will be shown when you run a command. Also, I showed two ways to get the full list of properties Get-Member (just the property name) and “Select-Object *” which also retrieve the values.
Thanks for reading!