PowerShell Prowess: The 3 commands you need to know 👈
Next to the Get-Command and hte Get-Help cmdlets, there are 3 other cmdlets that are essential for any PowerShell user. In this blog post I will explain my view on why they are essential and provide hands-on examples on how to use them.
Unveiling Object Properties and Methods with Get-Member
The official documentation states: The Get-Member cmdlet gets the members, the properties and methods, of objects. Making it the Swiss army knife for exploring the properties and methods of any object in PowerShell and thus indispensable for any PowerShell user.
Why you need to know
- Object Exploration -> When working with PowerShell objects it’s crucial to understand the object you are dealing with.
Get-Memberhelps to uncover the object’s properties, methods, and other characteristics.
- Discover Object Type ->
Get-Membercan be used to determine the type of an object. This is especially useful when working with pipelines, as different cmdlets return objects of varying types.
In this example the
Get-MgUser cmdlet is used to retrieve a user from the Microsoft Graph. Note the result:
Now note the
Get-Membercmdlet also returns the objects TypeName.
Tailoring Object Output with Select-Object
The official documentation states: Selects objects or object properties. The cherry picker for displaying and formatting specific properties of an objects. Making it an invaluable for formatting and refining data.
This is a versatile command that enables customization of the properties of an object’s output.
Why you need to know
- Data Shaping -> Tailoring object output to specific needs by including, excluding ordering and filtering properties. This is crucial for creating neat and organized objects.
- Efficient Scripting -> When working with complex objects, displaying all available properties with
Select-Object -Property *helps by making output transparent and easier to work with.
The example is a continuation of the previous one. First all properties are listed. Then a selection is made of the preferred properties. Finally the selected properties for the last 3 objects are returned.
The Full Name(space) of a Data Type with [DataType].FullName
Outside of folder and file paths this method is a less known one and requires a bit more explanation. PowerShell uses a concept called
Type Accelerators to make it easier to work with .NET types. A
Type Accelerator is a shortcut for a .NET type. For example
[int] is a shortcut for the .NET type
System.Int32. While the intent of the type accelerators is to shorten the names of .NET types there are reasons to use the full name of a type. Such is the case when using the
New-Object cmdlet to create an instance of a .NET type which can be retrieved by using the
Another consideration for using the full name comes from the best practice to avoid aliases (PSAvoidUsingCmdletAliases), since the type accelerators are aliases for .NET types some PowerShell devs (like myself) prefer using the .NET types.
To get a list of all available type accelerators use the following command:
That’s all folks! I hope you found the
Select-Object and the DataType Accelerators
.FullName commands as useful and need to know as I do. If you are interested in the reference material used to make this post, please visit the following links.
- learn.microsoft.com - Get-Member
- YouTube video by David Dalton - Exploring PowerShell objects with Get-Member
- PowerShell Basics: Getting More Information with Get-Member
- learn.microsoft.com - Select-Object
- YouTube video by David Dalton - Choosing properties with Select-Object
- Rudy Mens - How to use PowerShell Select-Object
- PowerShell Data Types & Type Accelerators: A Tutorial
- PowerShell practice and style
- Simplify Your Script by Creating PowerShell Type Accelerators
- Using PowerShell type accelerators
As always, a big thanks for reading this post. If you liked it, don’t be shy and have a look at my other posts .