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
-Filterwith incorrect WQL syntax, causing errors or no results. - Not specifying
-ComputerNamewhen querying remote machines, leading to local queries only. - Confusing
Get-CimInstancewithGet-WmiObject, whereGet-CimInstanceuses 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
| Parameter | Description |
|---|---|
| -ClassName | Specify the CIM class to query (e.g., Win32_OperatingSystem) |
| -Filter | Apply a WQL filter string to narrow results |
| -ComputerName | Target remote computer(s) by name or IP |
| -Namespace | Specify CIM namespace, default is root/cimv2 |
| -Property | Select 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.