PowerShell Check Disk Space - Win32_LogicalDisk
PowerShell Scripts - Check Disk with Win32_LogicalDisk
Our mission is to investigate and interrogate a computer's logical disk. Goals include discovering the size of each volume, and how much free space is available. We also have pure PowerShell goals, for example, to examine the 'Select' statement, and to control the display with -groupby, |sort and -auto.
Topics for Get-WmiObject Win32_LogicalDisk
Each new PowerShell command that you discover will benefit from being interrogated by what I call the 'Trusty Twosome'. I guarantee that if you research with Get-Help and Get-Member then you will reveal new scripting possibilities for Get-WmiObject Win32_LogicalDisk. Let us start with Get-Help.
Before we use PowerShell's Win32_LogicalDisk, let us investigate the master cmdlet Get-WmiObject. In particular, we need to understand the syntax of parameters such as -Class and -ComputerName. Incidentally, you can use the alias gwmi instead of Get-WmiObject.
# Help with PowerShell WMI object:
Note 1: If you prefer to see examples append -full, thus: help Get-WmiObject -full
How to Research Get-Wmi Object Disk Classes
# Check WmiObject Classes
Note 2: The above script is setup so that you can easily edit $Type, for example, "Win32_Disk". See more on the $_ .name variable.
# PowerShell cmdlet to display Logical Disk information
Those with a VBScript background may be more familiar with query and select. This achieves the same result by using just the -query parameter combined with an SQL-Like select statement.
# PowerShell cmdlet to display Logical Disk information
# Properties for PowerShell logical disk object:
Note 3: If you enjoy aliases then try: gwmi Win32_logicaldisk| gm.
Note 4: If you are fond of filters: gwmi Win32_logicaldisk| gm -Membertype property.
Get-Member (gm) reveals a whole host of properties that you don't normally see in the Disk Manager.
Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft's operating systems. Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.
Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try - it's free.
Following the above research, we now have extra information about the properties of Win32_logicaldisk, consequently we create a PowerShell script which collects disk space statistics.
# PowerShell command disk space
Note Out-GridView: PowerShell v 2.0 introduces a new cmdlet to control data display. See more on how to pipe the results into out-GridView.
Example 2a - Display Disk Size and FreeSpace
Warning! Confession! One day I was just playing and made this more script which achieves the same as the above, however it does prepare us for a more complicated script below.
In the example below, I introduce a variable called $Item to control the output. If you would like to refine the output to your specification, then research with Get-Member. The parameter -auto helps to 'tighten' the data display.
# PowerShell cmdlet to display a disk's free space
Example 2b - Controlling the Decimals
This script is a digression which introduces 'Expression' to calculate freespace. It also employs the -f format to control the decimal places (N0) and the percentage (P0).
Note 5: These scripts use localhost, you could change, or else remove this parameter. See also Get-PSDrive method.
SolarWinds' Network Performance Monitor will help you discover what's happening on your network. This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.
What I like best is the way NPM suggests solutions to network problems. Its also has the ability to monitor the health of individual VMware virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.
i) The following example provides structure to the data thanks to the -groupby command in the last line.
this example's neatest technical achievement is:
iii) It also introduces the idea of interrogating other machines on the network, for this it uses the -computerName, incidentally, -computer works just as well.
Naturally, the first thing to alter in the script below is:
# PowerShell Get-WmiObject disk space
The new feature of the example below is the 'Where MediaType' filter. By now I hope that you have started adjusting the script according to your needs.
# PowerShell check disk space
This Engineer's Toolset v10 provides a comprehensive console of 50 utilities for troubleshooting computer problems. Guy says it helps me monitor what's occurring on the network, and each tool teaches me more about how the underlying system operates.
There are so many good gadgets; it's like having free rein of a sweetshop. Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools. Try the SolarWinds Engineer's Toolset now!
The first purpose of this script is to remind you that WMI can access at least 500 win32 objects, of which 20 contain the word 'Disk'. The second purpose is to show what information is revealed by a specific object namely: Win32_DiskPartion.
# PowerShell cmdlet to display a disk's partition information.
Note 6: Remember Get-Member. Here is a classic situation to employ Get-Member to discover what other properties are available for fine-tuning $Item. This is the command to explore:
Get-WmiObject Win32_DiskPartition | Get-member.
Note 7: The basis of this script was kindly supplied by Jimmy May. Please send me your ideas for scripts. See more on disk problems.
Summary of PowerShell with Win32_LogicalDisk
The central feature of this page is the WMI command Get-WmiObject Win32_LogicalDisk. From that base we modify the script to select properties such as, FreeSpace and disk size. Along the journey we sharpen our skills to filter with 'Where', and also to collate the data with -groupby. The result is a neat PowerShell script which collects disk space information.
If you like this page then please share it with your friends
See More Microsoft PowerShell WMI Examples:
Please email me if you have a script examples. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.