Disk info tool

This topic contains 8 replies, has 6 voices, and was last updated by Profile photo of Graham Beer Graham Beer 7 months, 3 weeks ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #35206
    Profile photo of Graham Beer
    Graham Beer
    Participant

    Been reading through Ed Wilson's new book, Step by Step. Some really cool stuff in there and inspired me to have a play !
    Created a small function to obtain the local C: drive freespace and drive size, then format the data into a custom object adding a percent used tab.

    Am i on the right lines ?

    Function Get-DriveInfo { #Runs only for local device
    
    #Get drive information from WMI
    $FreeSpace = (get-wmiobject win32_logicaldisk).where{($PSItem.DeviceID -eq 'C:')} | select Freespace,Size
    
    #Create Object to Display Results
    $DriveInfo = [pscustomobject]@{
        Freespace = "{0}","GB" -f [math]::truncate($freespace.freespace / 1GB)
        DiskSize = "{0}","GB" -f [math]::truncate($freespace.size / 1GB) 
        PercentUsed = "{0:P}" -f ($freespace.Freespace / $freespace.Size)
        }
    
    #Display 
    $DriveInfo | ft -AutoSize
    
    }#End of Function
    

    The results are displayed like so,

    PS C:\Windows\system32> Get-DriveInfo

    Freespace DiskSize PercentUsed
    ——— ——– ———–
    48 GB 237 GB 20.25%

    Be good to get some feedback, thanks !

    #35208
    Profile photo of Wilfredo Perez
    Wilfredo Perez
    Participant

    Nice, but the function only get the C:\drive, how about if I have a d:\drive. Use parameters and advanced functions. Nice script.

    #35209
    Profile photo of Graham Beer
    Graham Beer
    Participant

    Thanks Wifredo, i'll make it a bit more dynamic.

    #35210
    Profile photo of Peter Jurgens
    Peter Jurgens
    Participant

    My only suggestion would be to filter with Get-WmiObject rather than using where-object to filter. This is always good practice, the mantra being 'filter left and format right'.

    #35211

    I'd make these changes:

    function Get-DriveInfo {

    $freespace = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DeviceId = 'C:'" |
    Select-Object Freespace, Size

    $props = [ordered]@{
    'Freespace(GB)' = [math]::Round($freespace.freespace / 1GB, 2)
    'DiskSize(GB)' = [math]::Round($freespace.size / 1GB, 2)
    PercentUsed = [math]::Round(($freespace.Freespace / $freespace.Size) * 100, 2)
    }

    New-Object -TypeName PSObject -Property $props
    }

    Use the CIM cmdlet rather than WMI cmdlet. Use the -Filter parameter rather than Where (already mentioned above)

    Change the properties so that you're outputting numbers rather than strings – put the units into the property name rather than as part of the value

    Output the object. You can always use Format-Table outside the function is need be. PowerShell 5 does a reasonable job of formatting table data automatically

    #35212
    Profile photo of Graham Beer
    Graham Beer
    Participant

    Great stuff, thanks Richard and Peter.
    Richard, any reason why you didn't use [PSCUSTOMOBJECT] ?

    #35227
    Profile photo of Graham Beer
    Graham Beer
    Participant

    I've added advanced function to it...

    
    function Get-DriveInfo {
    
        [CmdletBinding()]
        param (
            [ValidateScript({
                If ($_ -match "^[A-Za-z]\:+$") {
                    $True
                }
                else {
                    Throw "$_ is either not a valid drive or it is not written as 'C:'."
                }
            })]
            [string]$Drive
        )
    
    $freespace = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DeviceId = '$Drive'" |
     Select-Object Freespace, Size
    
    $props = [ordered]@{
     'Freespace(GB)' = [math]::Round($freespace.freespace / 1GB, 2)
     'DiskSize(GB)' = [math]::Round($freespace.size / 1GB, 2)
     Percentfree = [math]::Round(($freespace.Freespace / $freespace.Size) * 100, 2)
     }
    
    New-Object -TypeName PSObject -Property $props
     }
    
    #35230
    Profile photo of MichaelKF
    MichaelKF
    Participant

    Hi Graham,

    how about setting "C:" as default value for the Drive parameter?

    [string]$Drive="C:"

    By doing so, you still have the convenience of your first draft, but also the flexibility of your last version.

    Michael

    #35234
    Profile photo of Graham Beer
    Graham Beer
    Participant

    Nice idea Micheal, thanks !

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.