Querying and managing system information with WMI – PowerShell and Windows Management Instrumentation (WMI)

Querying and managing system information with WMI - PowerShell and Windows Management Instrumentation (WMI)

To query and manage system information using WMI and PowerShell, you can follow these steps:

  1. Open a PowerShell session with administrative privileges.
  2. Use the Get-WmiObject cmdlet to query WMI classes and retrieve system information. For example, to get information about the operating system, you can use the Win32_OperatingSystem class:
   $operatingSystem = Get-WmiObject -Class Win32_OperatingSystem
  1. Once you have retrieved the WMI object, you can access its properties and methods. For example, to get the operating system version and caption, you can use the following commands:
   $operatingSystem.Version
   $operatingSystem.Caption
  1. To filter the results, you can use the -Filter parameter with the Get-WmiObject cmdlet. For example, to get information about all running processes with “explorer” in the name, you can use the following command:
   $processes = Get-WmiObject -Class Win32_Process -Filter "Name LIKE '%explorer%'"
  1. To modify WMI objects, you can use the Set-WmiInstance cmdlet. Make sure you have the necessary permissions to modify the object. For example, to change the computer name, you can use the following command:
   $computerSystem = Get-WmiObject -Class Win32_ComputerSystem
   $computerSystem.Rename("NewComputerName")
  1. You can also invoke methods defined in the WMI classes using the Invoke-WmiMethod cmdlet. For example, to shut down a remote computer, you can use the following command:
   Invoke-WmiMethod -Class Win32_OperatingSystem -Name Shutdown -ComputerName "RemoteComputer" -ArgumentList 0
  1. Use the | (pipe) operator to chain multiple cmdlets together for more advanced queries and operations. For example, to get a list of running services and sort them by name, you can use the following command:
   Get-WmiObject -Class Win32_Service | Sort-Object -Property Name

Querying and managing system information with WMI using PowerShell can be a powerful way to retrieve data and perform administrative tasks on Windows systems. Here’s an overview of how you can use PowerShell and WMI to accomplish these tasks:

Querying System Information:
PowerShell provides the Get-WmiObject cmdlet (or Get-CimInstance in newer PowerShell versions) to query WMI and retrieve system information. You can specify the WMI class you want to query and use various filters and properties to refine the results.

For example, to retrieve information about the operating system, you can use the Win32_OperatingSystem class:


Get-WmiObject -Class Win32_OperatingSystem

This command retrieves system information such as the operating system version, build number, registered owner, and more.

Filtering and Selecting Properties:
You can filter the results of a WMI query using the Where-Object cmdlet and select specific properties using the Select-Object cmdlet.

For example, to retrieve only the names and capacities of all disk drives on the system, you can use:


Get-WmiObject -Class Win32_DiskDrive | Select-Object DeviceID, Model, Size

This command retrieves the device ID, model, and size properties of all disk drives.

Managing System Settings:
WMI also allows you to manage system settings, such as configuring network settings, modifying services, and more. You can use the appropriate WMI classes and methods to perform these tasks.

For example, to enable or disable a specific Windows service, you can use the Set-Service cmdlet along with WMI queries. Here’s an example that disables the Windows Update service:


$service = Get-WmiObject -Class Win32_Service | Where-Object { $_.Name -eq ‘wuauserv’ }
$service.ChangeStartMode(‘Disabled’)
Set-Service -Name ‘wuauserv’ -StartupType Disabled

This code retrieves the Windows Update service using the `Win32_Service` class, disables it by changing the start mode, and then uses `Set-Service` to update the service configuration.

Executing Methods:
WMI classes often have methods that allow you to perform specific actions on the associated objects. You can use PowerShell to invoke these methods.

For example, to initiate a system restart, you can use the Win32_OperatingSystem class and its Reboot method:


$operatingSystem = Get-WmiObject -Class Win32_OperatingSystem
$operatingSystem.Reboot()

This code retrieves the operating system information and invokes the Reboot method to initiate a restart.
These are just a few examples of how you can use PowerShell and WMI to query and manage system information. WMI provides a vast range of classes and methods that enable you to gather data, configure settings, and perform administrative tasks on Windows systems. You can explore the available WMI classes and their associated properties and methods to accomplish specific management requirements.

These are just a few examples of how you can query and manage system information using WMI and PowerShell. There are many more WMI classes and cmdlets available for various tasks. You can explore the available WMI classes and their properties using the WMI documentation or tools like the WMI Code Creator.

Remember to use caution and double-check your commands, especially when working with objects that have the potential to modify system settings. Always test your commands in a non-production environment before executing them in a live environment.

SHARE
By Albert

Leave a Reply

Your email address will not be published. Required fields are marked *

No widgets found. Go to Widget page and add the widget in Offcanvas Sidebar Widget Area.