Get-Member

Understanding PowerShell’s Get-Member Cmdlet

Welcome back to Wahmans Powershell blog! Today, we’re diving into one of the most essential cmdlets for anyone working with PowerShell: Get-Member. If you’ve ever asked, “What properties or methods does this object have?” then Get-Member is your go-to tool.

According to Microsoft, Get-Member gets the properties and methods of objects. This is incredibly useful when you’re working with results returned from cmdlets or scripts and want to understand what kind of data you’re handling.

Why is Get-Member Important?

PowerShell works with objects, not just text. When you run a cmdlet that returns data, it typically returns .NET objects. To fully take advantage of these objects, you need to know what members (properties and methods) they contain. Get-Member shows you exactly that.

Let’s look at four Get-Member examples, from beginner to advanced:

1. Basic Use: Inspecting a String Object

$str = "Hello, World!"
$str | Get-Member

This will list all the properties and methods associated with a System.String object. It’s a simple but powerful example of how you can examine what an object can do.

2. Finding Properties of a File Object

$file = Get-Item "C:\Windows\notepad.exe"
$file | Get-Member

This shows you all the properties (like Name, Length, Directory) and methods that are available on the file object.

3. Filtering Members by Type

$process = Get-Process | Select-Object -First 1
$process | Get-Member -MemberType Property

Here, we’re only interested in properties of a process object. -MemberType is very handy when you want to ignore methods or events.

4. Advanced: Exploring Custom Objects

$customObject = [PSCustomObject]@{
    Name = "Backup"
    Status = "Completed"
    Timestamp = (Get-Date)
}
$customObject | Get-Member

Custom objects are a big part of PowerShell scripting. With Get-Member, you can verify that your object has the structure you expect before passing it on in a pipeline or saving it to disk.

Wrapping Up

Get-Member is one of those foundational tools that continues to be useful no matter how advanced your scripts become. Whether you’re just starting out or building complex automations, understanding the objects you’re working with is key—and Get-Member is the window into that understanding.

Happy scripting, and I will see you in the next post!

Leave a Reply

Your email address will not be published. Required fields are marked *