Here are 10 useful PowerShell WMI (Windows Management Instrumentation) examples with explanations.
WMI allows you to retrieve and manage various system configurations, settings, and services on Windows systems.
Table of Contents
1. Get Operating System Information
You can retrieve detailed information about the operating system, such as version, architecture, and install date.
Get-WmiObject -Class Win32_OperatingSystem
Explanation: This retrieves information about the OS, such as version, architecture, free physical memory, and more.
2. Get CPU Information
To retrieve detailed CPU information like name, number of cores, and clock speed.
Get-WmiObject -Class Win32_Processor
Explanation: This command provides details about the processor(s) installed on the system, including the CPU name, core count, and speed.
3. Get Memory (RAM) Information
You can get information about the physical memory (RAM) installed in the system.
Get-WmiObject -Class Win32_PhysicalMemory
Explanation: This retrieves details about the physical memory, such as capacity, speed, and manufacturer.
4. Get Network Adapter Configuration
To gather detailed network adapter configurations like IP address, MAC address, and gateway.
Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where-Object { $_.IPEnabled -eq $true }
Explanation: This command retrieves network adapter information only for the adapters that are enabled with an IP address.
5. Get Disk Information
You can retrieve detailed information about the disk drives, including free space, total size, and volume label.
Get-WmiObject -Class Win32_LogicalDisk
Explanation: This shows information about all logical disks (drives) on the system, including their size, free space, and file system type.
6. Get Installed Software
You can list all installed software on a machine using the following command:
Get-WmiObject -Class Win32_Product
Explanation: This retrieves a list of installed software products along with their version numbers and installation paths.
7. Get BIOS Information
To obtain information about the BIOS, including its version and manufacturer.
Get-WmiObject -Class Win32_BIOS
Explanation: This retrieves BIOS information, including the version, manufacturer, and release date.
8. Get Service Status
To check the status of a Windows service using WMI.
Get-WmiObject -Class Win32_Service -Filter "Name='wuauserv'"
Explanation: This retrieves information about a specific Windows service (Windows Update in this case), such as whether it is running or stopped.
9. Reboot a Remote Machine
You can remotely reboot a machine by invoking a WMI method.
(Get-WmiObject -Class Win32_OperatingSystem -ComputerName "RemoteComputer").Win32Shutdown(6)
Explanation: This command shuts down and restarts the specified remote machine using the WMI shutdown method (6 stands for reboot).
10. Check for System Uptime
You can check how long the system has been running since its last boot.
(Get-WmiObject -Class Win32_OperatingSystem).LastBootUpTime
Explanation: This retrieves the last time the system was booted, allowing you to calculate the uptime.
How WMI Works with PowerShell
WMI allows access to system management data in a standardized way. In PowerShell, the Get-WmiObject cmdlet is used to query WMI classes and objects.
You can filter, format, and manipulate the output to gather the necessary system information. WMI classes like Win32_OperatingSystem or Win32_Processor represent specific types of data (e.g., OS, CPU), and Get-WmiObject helps fetch details from those classes.
Example Workflow:
For example, to retrieve information about the system and its components like the CPU, disk, and network, you could use a script combining multiple WMI queries:
$os = Get-WmiObject -Class Win32_OperatingSystem $cpu = Get-WmiObject -Class Win32_Processor $memory = Get-WmiObject -Class Win32_PhysicalMemory $disk = Get-WmiObject -Class Win32_LogicalDisk Write-Host "OS Name: " $os.Caption Write-Host "CPU: " $cpu.Name Write-Host "Memory: " ($memory.Capacity / 1GB) " GB" Write-Host "Disk Free Space: " ($disk.FreeSpace / 1GB) " GB"
This would provide a quick overview of the system’s operating system, CPU, memory, and disk space.
Conclusion:
WMI is a powerful tool for managing and retrieving system information on Windows systems via PowerShell. By combining various WMI classes and cmdlets, you can automate administrative tasks, gather detailed system data, and even perform remote management operations.