Powershell

Table of Contents

WMI

Windows Management Instrumentation (WMI) is a core technology for Windows system administration because it exposes a wide range of information in a uniform manner. Because of how much WMI makes possible, the Windows PowerShell cmdlet for accessing WMI objects, Get-WmiObject, is one of the most useful for doing real work. We are going to discuss how to use Get-WmiObject to access WMI objects and then how to use WMI objects to do specific things.

Note: For Powershell 6.0 Core users, as WMI is Microsofts own operating system internal configuration this command is not available for Linux and macOS operating systems. In this case use the Get-CimClass command to view classes and Get-CimInstance to query the class. They are the same commands but are a wrapper into generically querying the Host operating system.

Microsoft will be depreciating support of WMI for Powershell over the new CIM classes as CIM is the way forward being agnostic to host operating system that Powershell 6.0 core is installed on.
Follow the below links to view further from Microsoft Docs on how to use CIM classes.

Get-CimClass - https://docs.microsoft.com/en-us/powershell/module/cimcmdlets/get-cimclass?view=powershell-6
Get-CimInstance - https://docs.microsoft.com/en-us/powershell/module/cimcmdlets/get-ciminstance?view=powershell-6

Listing WMI Classes

The first problem most WMI users encounter is trying to find out what can be done with WMI. WMI classes describe the resources that can be managed. There are hundreds of WMI classes, some of which contain dozens of properties. Get-WmiObject addresses this problem by making WMI discoverable. You can get a list of the WMI classes available on the local computer by typing:

Get-WmiObject -List




You can retrieve the same information from a remote computer by using the ComputerName parameter, specifying a computer name or IP address:

Get-WmiObject -List -ComputerName 192.168.1.10

Remote WMI of external devices

When using Get-WmiObject to connect to a remote computer, the remote computer must be running WMI and, under the default configuration, the account you are using must be in the local administrators group on the remote computer. The remote system does not need to have Windows PowerShell installed. This allows you to administer operating systems that are not running Windows PowerShell, but do have WMI available.

WMI Classes

If you already know the name of a WMI class, you can use it to get information immediately. For example, one of the WMI classes commonly used for retrieving information about a computer is Win32_OperatingSystem

To try along you may replace the -ComputerName parameter with your own computer as localhost. depending on your environment setup if the code fails with authentication error. Make sure Powershell is running as administrator. By right clicking on Software in Start menu and click run as admin. If the code still fails then you will then also need to specify the credentials parameter (-credentials) with your password of your local computer that you are running this shell on.

Get-WmiObject -Class Win32_OperatingSystem -Namespace root/cimv2 -ComputerName localhost


The Win32_OperatingSystem class has many more properties than those displayed here. You can use Get-Member to see all the properties. The properties of a WMI class are automatically available like other object properties. There may be an instance where you are not seeing all the available properties and methods that are available through get-member command, in this case use the force parameter (-Force) to see hidden attributes.

Get-WmiObject -Class Win32_OperatingSystem -Namespace root/cimv2 -ComputerName localhost | Get-Member -Force




Remote PowerShell Sample script

Below example us running the powershell script remotely from another shell powershell flatform. Enter your script inside the curly brackets, below is getting the Office Activation key.

Note: type the below as one line

start C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe -NoProfile -NonInteractive -Command "&
{wmic path SoftwareLicensingService get OA3xOriginalProductKey > C:\test.txt}"

Visit next page to learn about - Numbered Lists