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 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: WMI Monitor and It's Free!
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft 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.
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
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 YourMachine -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 YourMachine -query ` "Select $([string]::Join(',',$Item))
from Win32_logicaldisk ` Where MediaType=12" | sort MediaType, DeviceID
` | Format-Table $item -auto
Guy Recommends: Solarwinds' Free Bulk Import Tool
Import users from a spreadsheet. Just provide a list of the
users with their fields in the
top row, and save as .csv file. Then launch this FREE utility and match
your fields with AD's
attributes, click to import the users. Optionally, you can
provide the name of the OU where the new accounts will be born.
There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:
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 5: 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 6: The basis of this script was kindly supplied
by Jimmy May. Please send me your ideas for scripts.
»
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 one of the hidden
treasures of Microsoft operating systems.
Fortunately, Solarwinds
have created the
Free WMI Monitor so that you can actually see and understand these gems of
performance information. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.