Ad Space here

Welcome Guest! To enable all features please Login or Register.



Go to last post Go to first unread
Dustin Higgins  
#1 Posted : Sunday, March 31, 2019 8:32:42 PM(UTC)
Dustin Higgins

Rank: Advanced Member

Groups: Registered
Joined: 7/1/2018(UTC)
Posts: 64
_United States

Thanks: 1 times
Was thanked: 6 time(s) in 6 post(s)

What time is it?  It is object time!  When I first learned PowerShell, I had a scripting background.  I didn't really understand the Object Oriented nature of PowerShell.  It wasn't until one of my friends and coworker beat me over the head with an PowerShell book did I grasp the concept of objects and PowerShell.  Once I got it, literally everything became an object.  What a great thing!

This post is a continuation of  Workstation Preventative Maintenance - Disk Space


In this post I am going to focus on what to collect regarding disk space and how to store it in PowerShell custom objects.

One of the great things about PowerShell is the objects and their properties, etc.  For example, the following WMI query command returns objects from a remote computer named DHB0001:

Get-WmiObject win32_logicaldisk -ComputerName DHB0001

  • ​​​​​​​DeviceID     : C:

  • DriveType    : 3

  • ProviderName :

  • FreeSpace    : 823135649792

  • Size         : 999559262208

  • VolumeName   :

The problem with the above command is that it is hard to quickly identify which computers are issues when it comes to low disk space.  Especially when computers have different size hard drives and the Size/FreeSpace numbers are not in the format that humans are used to dealing with.  Most of us speak GB these days.  Creating a custom PowerShell objects is a nice way to solve that issue.  Here is an example:

# Create a summary object for the computer or server
$DiskSummaryObj = [PSCustomObject]@{
    Computer = "DHB001"
    C_Size = $null
    C_UsedSpace = $null
    C_FreeSpace = $null
    C_PercentFree = $null
    LogicalDiskObj = @()
    DateCreated = (get-date)
# Query WMI
$AllLogicalDisks = Get-WmiObject win32_logicaldisk -ComputerName $DiskSummaryObj.Computer
# Since there can be multiple drives, loop through them all
ForEach ($LogicalDisk In $AllLogicalDisks) {
    # We only care about Fixed (#3)
    If ($LogicalDisk.DriveType -eq 3) {
        # Go ahead and do the math!  Simple but really helpfull!
        $FreeGB = [math]::Round(($LogicalDisk.FreeSpace / 1GB),2)
        $SizeGB = [math]::Round(($LogicalDisk.Size / 1GB),2)
        # Create a disk object for each drive
        $DiskObj = [PSCustomObject]@{
            DeviceID = $LogicalDisk.DeviceID
            DriveType = $LogicalDisk.DriveType
            ProviderName = $LogicalDisk.ProviderName
            FreeSpace = $LogicalDisk.FreeSpace
            FreeSpace_GB = $FreeGB
            Size = $LogicalDisk.Size
            Size_GB = $SizeGB
            VolumeName = $LogicalDisk.VolumeName
        # If it is the main drive, add to the summary object
        If ($DiskObj.DeviceId -eq "C:") { 
            $DiskSummaryObj.C_Size = $sizeGB
            $DiskSummaryObj.C_UsedSpace = [math]::Round(($sizeGB - $freeGB),2)
            $DiskSummaryObj.C_FreeSpace = $freeGB
            $DiskSummaryObj.C_PercentFree = [math]::Round(($freeGB / $sizeGB) * 100,2)   
        # Add the Disk Object to the Summary Object LogicalDiskObj array
        $DiskSummaryObj.LogicalDiskObj += $DiskObj

} # Return the object $DiskSummaryObj

With everything in a custom summary object, it is now easy to focus on a threshold such as computers with less than 10% free space.  The disk space numbers are also in GB which is easier to comprehend.

  • Computer       : DHB0001

  • C_Size         : 930.91

  • C_UsedSpace    : 164.31

  • C_FreeSpace    : 766.6

  • C_PercentFree  : 82.35

  • LogicalDiskObj : {@{DeviceID=C:; DriveType=3; ProviderName=; FreeSpace=823131680768; FreeSpace_GB=766.6;

                 Size=999559262208; Size_GB=930.91; VolumeName=}}

  • DateCreated    : 3/31/2019 7:29:33 PM

​​​​​​​The next post will discuss efficient methods to collect the disk space data.



Edited by user Sunday, March 31, 2019 9:05:39 PM(UTC)  | Reason: Not specified

Ad Space here
Rss Feed  Atom Feed
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.