0
0
PowershellHow-ToBeginner · 3 min read

How to Use Get-CimInstance in PowerShell: Syntax and Examples

Use Get-CimInstance in PowerShell to query management information from local or remote computers using CIM (Common Information Model). It retrieves instances of CIM classes like Win32_OperatingSystem and outputs objects you can filter or format.
📐

Syntax

The basic syntax of Get-CimInstance includes specifying the CIM class you want to query and optional parameters for filtering or targeting remote computers.

  • -ClassName: The CIM class to query (required).
  • -Filter: A WQL filter string to limit results.
  • -ComputerName: Target remote computer(s).
  • -Namespace: CIM namespace, default is root/cimv2.
powershell
Get-CimInstance -ClassName <ClassName> [-Filter <Filter>] [-ComputerName <ComputerName>] [-Namespace <Namespace>]
💻

Example

This example retrieves information about the operating system on the local computer using the Win32_OperatingSystem CIM class.

powershell
Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object Caption, Version, BuildNumber
Output
Caption Version BuildNumber ------- ------- ----------- Microsoft Windows 10 Pro 10.0.19045 19045
⚠️

Common Pitfalls

Common mistakes include:

  • Using -Filter with incorrect WQL syntax, causing errors or no results.
  • Not specifying -ComputerName when querying remote machines, leading to local queries only.
  • Confusing Get-CimInstance with Get-WmiObject, where Get-CimInstance uses newer CIM cmdlets and is preferred.
powershell
## Wrong filter syntax example (missing quotes)
Get-CimInstance -ClassName Win32_Process -Filter ProcessId = 1234

## Correct filter syntax example
Get-CimInstance -ClassName Win32_Process -Filter "ProcessId = 1234"
📊

Quick Reference

ParameterDescription
-ClassNameSpecify the CIM class to query (e.g., Win32_OperatingSystem)
-FilterApply a WQL filter string to narrow results
-ComputerNameTarget remote computer(s) by name or IP
-NamespaceSpecify CIM namespace, default is root/cimv2
-PropertySelect specific properties to retrieve

Key Takeaways

Use Get-CimInstance to query system info via CIM classes locally or remotely.
Always enclose filter expressions in quotes to avoid syntax errors.
Get-CimInstance is the modern replacement for Get-WmiObject and supports newer protocols.
Specify -ComputerName to query remote machines; otherwise, it queries locally.
Use Select-Object to pick useful properties from the output for clarity.