> ## Documentation Index
> Fetch the complete documentation index at: https://tally.wharflab.com/llms.txt
> Use this file to discover all available pages before exploring further.

# powershell/PSAvoidUsingWMICmdlet

> Avoid Using Get-WMIObject, Remove-WMIObject, Invoke-WmiMethod, Register-WmiEvent, Set-WmiInstance

`powershell/PSAvoidUsingWMICmdlet` is a PSScriptAnalyzer diagnostic emitted by tally for PowerShell snippets embedded in Dockerfiles.

| Property | Value            |
| -------- | ---------------- |
| Severity | Warning          |
| Category | PSScriptAnalyzer |
| Auto-fix | No               |

## Description

As of PowerShell 3.0, the CIM cmdlets should be used over the WMI cmdlets.

The following cmdlets should not be used:

* `Get-WmiObject`
* `Remove-WmiObject`
* `Invoke-WmiMethod`
* `Register-WmiEvent`
* `Set-WmiInstance`

Use the following cmdlets instead:

* `Get-CimInstance`
* `Remove-CimInstance`
* `Invoke-CimMethod`
* `Register-CimIndicationEvent`
* `Set-CimInstance`

The CIM cmdlets comply with WS-Management (WSMan) standards and with the Common Information Model
(CIM) standard, allowing for the management of Windows and non-Windows operating systems.

## How

Change to the equivalent CIM-based cmdlet.

* `Get-WmiObject` -> `Get-CimInstance`
* `Remove-WmiObject` -> `Remove-CimInstance`
* `Invoke-WmiMethod` -> `Invoke-CimMethod`
* `Register-WmiEvent` -> `Register-CimIndicationEvent`
* `Set-WmiInstance` -> `Set-CimInstance`

## Examples

### Problematic code

```powershell theme={null}
Get-WmiObject -Query 'Select * from Win32_Process where name LIKE "myprocess%"' | Remove-WmiObject
Invoke-WmiMethod -Class Win32_Process -Name 'Create' -ArgumentList @{ CommandLine = 'notepad.exe' }
```

### Correct code

```powershell theme={null}
Get-CimInstance -Query 'Select * from Win32_Process where name LIKE "myprocess%"' | Remove-CIMInstance
Invoke-CimMethod -ClassName Win32_Process -MethodName 'Create' -Arguments @{ CommandLine = 'notepad.exe' }
```

## Source

This rule documentation is adapted from Microsoft's PSScriptAnalyzer documentation for
[AvoidUsingWMICmdlet](https://github.com/MicrosoftDocs/PowerShell-Docs-Modules/blob/main/reference/docs-conceptual/PSScriptAnalyzer/Rules/AvoidUsingWMICmdlet.md),
licensed under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/).
