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.
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: Get-Help Get-WmiObject
Note 1: If you prefer to see examples append
-full, thus: help Get-WmiObject -full
# PowerShell cmdlet to display Logical Disk information
Clear-Host Get-WmiObject Win32_logicaldisk | Format-Table
-auto
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
Clear-Host Get-WmiObject -query "Select * from Win32_logicaldisk" |Ft
# Properties for PowerShell logical disk object: Get-WmiObject
Win32_logicaldisk | Get-Member
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.
Guy
Recommends: Free WMI Monitor for PowerShell
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 Get-WmiObject Win32_logicaldisk ` | Format-Table DeviceId, MediaType,
Size, FreeSpace -auto
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 $Item = @("DeviceId", "MediaType", "Size", "FreeSpace") # Next follows one command split over two lines by a backtick `
Get-WmiObject -query "Select * from Win32_logicaldisk" `
| Format-Table $Item -auto
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.
Guy Recommends: A Free Trial of the Network Performance Monitor
(NPM)
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.
ii) However,
this example's neatest technical achievement is: Select $([string]::Join(',', $Item)). This means we can use one variable, $Item, to control both the properties for the select statement, and the
output of Format-Table.
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: -computer YourMachine.
# PowerShell Get-WmiObject disk space
Clear-Host $Item = @("DeviceId", "MediaType", "Size", "FreeSpace") # Next follows one command,
which is split over three lines by the backtick ` Get-WmiObject -computer
LocalHost -query ` "Select $([string]::Join(',',
$Item)) from Win32_logicaldisk" ` | Sort MediaType, DeviceID | Format-Table $item -auto -groupby MediaType
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 $Item = @("DeviceId", "MediaType", "Size", "FreeSpace")
Clear-Host # Next follows one command split over four lines by a backtick `
Get-WmiObject -computer LocalHost -query ` "Select $([string]::Join(',',$Item))
from Win32_logicaldisk ` Where MediaType=12" | Sort-Object MediaType, DeviceID
` | Format-Table $item -auto
Guy Recommends: SolarWinds Engineer's Toolset v10
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. Clear-Host $Item = @("Name","DiskIndex", "StartingOffset", "Bootable", "BlockSize", "NumberOfBlocks")
Get-WmiObject -query "Select * from
Win32_DiskPartition" | Format-Table $item -auto
Learning Points
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
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.
Windows Management Instrumentation (WMI) is
most useful for PowerShell scripting.
SolarWinds
have produced this
Free WMI Monitor to take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.