CHAPTER 4
One of the most striking features of PowerShell is its ability to interact with almost all settings of a machine. These settings are accessible through the WMI class. In these WMI examples, I will only use Win32 classes. However, in order to access my machine configurations, there is an endless list of them. A list of all of the classes is available here.
The WMI is an infrastructure management data and operations for Windows operating systems. It not only works with PowerShell, but also other languages such as C/C++, VB, and most other scripting languages built for Windows systems.
As I previously mentioned, I will only use Win32 classes in my example. These classes provide a large number of machine interactions such as getting the disk space available or getting our RAM memory usage. In the following table, you can see the main Win32 classes groups.
Table 6: Win32 Classes Groups
Class Group | Definition |
Computer System Hardware Classes | Hardware-related objects. |
Installed Applications Classes | Software-related objects. |
Operating System Classes | Operating system-related objects. |
Performance Counter Classes | Raw and calculated performance data from performance counters. |
Security Descriptor Helper Class | Class that provides methods to convert security descriptors between different formats. |
WMI Service Management Classes | Management for WMI. |
To use the WMI class in PowerShell, you need to use the command Get-WmiObject followed by the class name. The following code block shows an example of a WMI class invocation.
Get-WmiObject Win32_DiskDrive |
Calling this command will result in a set of information regarding your installed disks and its partitions.

Calling Get-WmiObject Win32_DiskDrive
WMI has an additional feature that is usually not explored by users called WMI´s WQL language, which allows you to query any WMI class. To use it, you must reference the WmiSearcher object. The following code block shows you how you can use it in your PowerShell scripts:
#Instantiate a WmiSearcher object with the query you wish to run. $query = [WmiSearcher]"select * from Win32_DiskDrive where Size>300000000000" #Run the query to retrieve the result. $query.Get() |
The previous script retrieves all disks in your machine that have a size greater than a predefined size. If you run it in the interactive shell, the result will resemble Figure 39.

Run WQL queries
For this challenge, we will setup a system administration scenario. One of the tasks of a professional in this area is to monitor the disk space of the various servers and make decisions and actions to avoid disk space allocation problems.
Performing this task manually is mundane and requires constant monitoring in order to avoid problems, so it is an ideal task to automate. In this first tutorial, we will create a simple script that lets you monitor available disk space. What I always try to encourage is establishing a scheduled task that runs every day, so that you won´t need to execute the script manually. Setting up a scheduled task that runs every day at 8am and saves the result to a log file, or even send you an alert email when disk space is running low, will give you free time to think about real problems instead of wasting time with monitoring and other routine tasks.
First create the basic routine, the function that will provide the information about a single machine. The following code block shows you how to create a function to retrieve this machine information.
function GetDiskInfo($serverName){ Get-WMIObject -ComputerName $serverName Win32_LogicalDisk | ?{($_.DriveType -eq 3)}| #Select which attribute to show. select @{n='Computer' ;e={"{0:n0}" -f ($serverName)}}, @{n='Drive' ;e={"{0:n0}" -f ($_.name)}}, @{n='Capacity (Gb)' ;e={"{0:n2}" -f ($_.size/1gb)}}, @{n='Free Space (Gb)';e={"{0:n2}" -f ($_.freespace/1gb)}}, @{n='Percentage Free';e={"{0:n2}%" -f ($_.freespace/$_.size*100)}}
} |
Now that we have the main function, we just need to create a list of all of the servers in our domain that we need to monitor, and then iterate through each one and call our GetDiskInfo function.
In the following code block, you have the full script to list all of your servers’ disk space with formatted values.
function GetDiskInfo($serverName){ Get-WMIObject -ComputerName $serverName Win32_LogicalDisk | ?{($_.DriveType -eq 3)}| #Select which attribute to show. select @{n='Computer' ;e={"{0:n0}" -f ($serverName)}}, @{n='Drive' ;e={"{0:n0}" -f ($_.name)}}, @{n='Capacity (Gb)' ;e={"{0:n2}" -f ($_.size/1gb)}}, @{n='Free Space (Gb)';e={"{0:n2}" -f ($_.freespace/1gb)}}, @{n='Percentage Free';e={"{0:n2}%" -f ($_.freespace/$_.size*100)}}
} #List of servers to monitor. $allServers = "ruimachado","computer2","computer3" #Iterate each server. $allServers | %{ GetDiskInfo -serverName $_ } |
The result of this script execution is a list of all disks with the respective free space for each of the servers declared in the array.

Disk Available List
You might not like how the information is being shown, and you might prefer a text file containing all of this information. To create this, you can store the information retrieved by the GetDiskInfo call inside of a foreach loop and then export it as a file.
#GetDiskInfo function goes here. $allinfoArray=@() #Iterate each server. $allServers | %{ $allinfoArray += GetDiskInfo -serverName $_ } $allinfoArray | Format-Table | Out-File -FilePath "c:\temp\diskLog.txt" |
This will generate a formatted table inside of a text file, as shown in Figure 41.

Formatted File with Disk Space