Guy recommends :
Free Solarwinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



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

 ♣

Trusty Twosome (Get-Help and Get-Member)

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.

Preparation with PowerShell's 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

How to Research Get-Wmi Object Disk Classes

# Check WmiObject Classes
Clear-Host
$Type = "Disk"
Get-WmiObject -list | Where-Object {$_.name -match $Type}

Note 2: The above script is setup so that you can easily edit $Type, for example, "Win32_Disk".  See more on the $_ .name variable.

Example 1: To Display Logical Disk Information

# 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

Research Properties with Get-Member

# 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!Solarwinds Free WMI Monitor

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.

Download your free copy of WMI Monitor

Example 2 - Display Disk Size and FreeSpace

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

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
$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 3: PowerShell Innovations

 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

Example 3b: Where Command Added

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 ToolFree Download of Solarwinds  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:

  1. Bulk-import new users into Active Directory.
  2. Seek and zap unwanted user accounts.
  3. Find inactive computers.

Download your FREE bulk import tool.

Example 4: Win32_DiskPartition

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

 


See More Microsoft PowerShell WMI Examples:

Home   • PowerShell Get-WmiObject   • WMI Services   • Win32_ComputerSystem

WMI Class  • [WMI] Type  • Win32_printer   • Win32_product   • SystemRestore

WMI Disk   • DNS   • PowerShell -Filter   • Windows PowerShell   • Memory

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.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

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.

Download your free copy of WMI Monitor

 

Home Copyright © 1999-2012 Computer Performance LTD All rights reserved

Please report a broken link, or an error.