Connecting to WMI via PowerShell

There are two methods to interact with WMI through PowerShell

  • DCOM: RPC over IP will be used for connecting to WMI.
    • ports 135/TCP, 49152-65535/TCP like sc.exe
  • Wsman: WinRM will be used for connecting to WMI.
    • ports 5985/TCP (WinRM HTTP) or 5986/TCP (WinRM HTTPS).

Initiate WMI session

$Opt = New-CimSessionOption -Protocol DCOM # or wsman
$Session = New-Cimsession -ComputerName TARGET -Credential $credential -SessionOption $Opt -ErrorAction Stop