Get-CimAssociatedInstance

Exploring Get-CimAssociatedInstance: Uncover Related CIM Data

Welcome to Wahmans Powershell blog! In today’s post, we’re diving into an incredibly useful cmdlet when working with the Common Information Model (CIM): Get-CimAssociatedInstance.

According to Microsoft’s documentation, this cmdlet Retrieves the CIM instances that are connected to a specific CIM instance by an association. This is especially useful when you’re exploring WMI (Windows Management Instrumentation) relationships in your system like connected devices, services, and configuration settings tied to objects.

Let’s go from basic to advanced with 4 examples to show how powerful Get-CimAssociatedInstance can be.

🔰 Example 1: List All Logical Disks Associated With a Computer System

This basic example retrieves all logical disks connected to the local computer system:

$computerSystem = Get-CimInstance -ClassName Win32_ComputerSystem

$logicalDisks = Get-CimAssociatedInstance -InputObject $computerSystem -ResultClassName Win32_LogicalDisk

$logicalDisks | Select-Object DeviceID, VolumeName, DriveType

This gives an overview of drive letters and types tied to your system.

🔧 Example 2: Find Network Adapters Associated with a Network Adapter Configuration

Each network adapter configuration has an associated adapter. Retrieve them like this:

$adapterConfigs = Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration | Where-Object { $_.IPEnabled -eq $true }

foreach ($config in $adapterConfigs) {
    $adapter = Get-CimAssociatedInstance -InputObject $config -ResultClassName Win32_NetworkAdapter
    Write-Output "Adapter Name: $($adapter.Name)"
}

This outputs the actual network adapter related to the configuration object.

🛠️ Example 3: Get Services Associated with a Specific Process

Want to know which services are running under a process like svchost? Try this:

$process = Get-CimInstance -ClassName Win32_Process | Where-Object { $_.Name -eq "svchost.exe" } | Select-Object -First 1

$services = Get-CimAssociatedInstance -InputObject $process -ResultClassName Win32_Service

$services | Select-Object Name, State

This shows services that are hosted within the specified process.

🚀 Example 4: Advanced — Map Physical Disks to Logical Disks Using Associations

This more complex example traces from logical disk to partition to physical disk:

$logicalDisks = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType = 3"

foreach ($logicalDisk in $logicalDisks) {
    $partitions = Get-CimAssociatedInstance -InputObject $logicalDisk -ResultClassName Win32_DiskPartition

    foreach ($partition in $partitions) {
        $physicalDisks = Get-CimAssociatedInstance -InputObject $partition -ResultClassName Win32_DiskDrive

        foreach ($disk in $physicalDisks) {
            Write-Output "Logical Disk: $($logicalDisk.DeviceID) -> Physical Disk: $($disk.Model) ($($disk.DeviceID))"
        }
    }
}

This is powerful when diagnosing storage issues or documenting hardware relationships on a system.

💡Wrapping Up

Get-CimAssociatedInstance is a valuable cmdlet when navigating WMI relationships in your environment. Whether it’s disks, services, or network components, understanding associations gives you deeper insights into how components relate to each other.

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 *