Skip to content

Instantly share code, notes, and snippets.

@sstorie
Forked from ScriptAutomate/WMI-CIM-CodeExamples.ps1
Created December 6, 2021 18:28
Show Gist options
  • Save sstorie/f16a580a373570815eae678f793312bd to your computer and use it in GitHub Desktop.
Save sstorie/f16a580a373570815eae678f793312bd to your computer and use it in GitHub Desktop.
Examples of Using WMI/CIM Cmdlets
break #To prevent accidental execution of all commands
# WMI cmdlets: Work against anything, where DCOM RPC dynamic port range is available
# CIM cmdlets: Exist in PowerShell v3 and up, can use DCOM or WSMAN. Can have CimSessions. Microsoft going forward.
$Creds = Get-Credential
Get-WmiObject -Class win32_computersystem
Get-WmiObject -Class win32_computersystem -ComputerName server1 -Credential $Creds
Get-CimInstance -Class win32_computersystem -ComputerName server1 -Credential $Creds
$Session = New-CimSession -ComputerName server1 -Credential $Creds
Get-CimInstance -Class win32_computersystem -CimSession $Session
Get-CimSession
Get-CimSession | Remove-CimSession
Get-Cimsession
$Option = New-CimSessionOption -Protocol Wsman #Can use DCOM, though! Wsman, single port, is default
$Session = New-CimSession -Credential $Creds -ComputerName server1 #-SessionOption $Option
$OSInfo = Get-CimInstance -Class win32_operatingsystem -CimSession $Session
# Find property with full name of Windows OS
$OSInfo.Caption
$OSInfo | Select Caption
# Create a spreadsheet with win32_logicaldisk:
## Title of partition (ex. C: or D:) as 'Name' header/property, only local disks
## Server's name as 'PSComputerName' header/property
## Type of filesystem (ex. NTFS) as 'FileSystem' header/property
## Freespace left, in GB, as 'FreeSpace(GB)'
Get-CimInstance -CimSession $Session -Filter 'DriveType LIKE "3"' |
select PSComputerName,
FileSystem,
@{Name='FreeSpace(GB)';Expression={$_.FreeSpace / 1GB -as 'int'}}, #Rounds float into int value
@{Name='Name';Expression={$_.DeviceID}}
Get-CimSession | Remove-CimSession
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment