Exploring Logical Disk Information with PowerShell and WMI
When managing a network of computers, it's crucial to keep an eye on various system parameters, including the state of the hard drives. PowerShell, with its powerful scripting capabilities, can help you gather essential information about logical disks on remote computers using the Windows Management Instrumentation (WMI) infrastructure. In this blog post, we'll explore a PowerShell script that retrieves and formats data about logical disks on a remote computer.
Understanding the Script
Let's dissect the PowerShell script step by step:
Get-WmiObject -Class Win32_LogicalDisk -ComputerName "pc1" |
Select-Object PSComputerName,
@{
name='DriveLetter(GB)';
expression={$PSItem.DeviceID}
},
@{
name='FreeSpace(GB)';
expression={$PSItem.FreeSpace / 1GB -as [int]}
},
@{
name='Size(GB)';
expression={$PSItem.Size / 1GB -as [int]}
},
@{
name='FreePercent';
expression={$PSItem.FreeSpace / $PSItem.Size * 100 -as [int]}
} |
Format-Table -AutoSize
Result:
Step 1: Retrieving Logical Disk Data
The script begins by using the Get-WmiObject
cmdlet to query information about logical disks on a remote computer named "bccmgr01." It specifically targets the Win32_LogicalDisk
WMI class, which contains data about logical disks on Windows systems.
Step 2: Selecting and Formatting Data
After retrieving data about the logical disks, we use the Select-Object
cmdlet to choose and format specific properties of the retrieved data. Here's what we're doing:
PSComputerName
: We include this property to display the name of the remote computer from which the data is retrieved.DriveLetter(GB)
: This calculated property extracts the "DeviceID" property from the retrieved data, representing drive letters, and renames it as "DriveLetter(GB)."FreeSpace(GB)
: This calculated property calculates the free space on each logical disk in gigabytes by dividing the "FreeSpace" property by 1GB and converting the result to an integer.Size(GB)
: Similar to "FreeSpace(GB)," this property calculates the total size of each logical disk in gigabytes by dividing the "Size" property by 1GB and converting the result to an integer.FreePercent
: This property calculates the percentage of free space on each logical disk by dividing the "FreeSpace" by the "Size" and multiplying the result by 100. The result is then converted to an integer.
Step 3: Formatting and Display
Finally, the Format-Table
cmdlet is used to format the selected properties in a table. The -AutoSize
parameter adjusts the column widths automatically based on the content, ensuring a clean and readable output.
Executing the Script
To execute this script on your own system, make sure you have PowerShell installed and replace "bccmgr01" with the name of the remote computer you want to query. Simply copy and paste the script into a PowerShell session or save it as a .ps1
file and run it.
Conclusion
With this PowerShell script, you can easily gather and present important information about logical disks on remote computers in a clear and organized table format. This can be invaluable for monitoring disk health, tracking storage usage, and making informed decisions about system maintenance and upgrades. PowerShell's versatility and WMI's capabilities make it a powerful combination for system administrators and IT professionals.
Happy scripting!
Subscribe to my newsletter
Read articles from modesto Tejeda directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
modesto Tejeda
modesto Tejeda
Experienced System Administrator with a computer programming degree and prior teaching experience as a college Programming Instructor, currently serving in the local government of Tampa, Florida.