Winter Scripting Games 2013 - Practice Event 1

The Winter Scripting Games 2013 started last week with the first practice event.

Unfortunately for me, I was not able to post my script on time. The first Practice event was due on the Tuesday, 5th of February... 11:59 PM GMT! I assume I had until midnight my time..... (My timezone is East GMT-5) 

If you want to join the Winter Scripting Games, check the following TheScriptingGames website for more information and the The Competitor Guide.

Summary: Practice Event 1 - Sizing up the Disks
You have been asked to create a Windows PowerShell advanced function named Get-DiskSizeInfo. It must accept one or more computer names, and use WMI or CIM to query each computer. For each computer, it must display the percentage of free space, drive letter, total size in gigabytes, and free space in gigabytes. If a specified computer cannot be contacted, the function must log the computer name to C:\Errors.txt and display no error message.
Minimum Requirements 
Optional Criteria
  • Display optional verbose output showing the computer name being contacted

My Script

Function Get-DiskSizeInfo {
        Check the Disk(s) Size and remaining freespace.

        .PARAMETER ComputerName
        Specify the computername(s)



        Get the drive(s), Disk(s) space, and the FreeSpace (GB and Percentage)

        Get-DiskSizeInfo -ComputerName SERVER01,SERVER02

        Get the drive(s), Disk(s) space, and the FreeSpace (GB and Percentage)
        on the Computers SERVER01 and SERVER02

        Get-Content Computers.txt | Get-DiskSizeInfo

        Get the drive(s), Disk(s) space, and the FreeSpace (GB and Percentage)
        for each computers listed in Computers.txt

        NAME  : Get-DiskSizeInfo
        AUTHOR: Francois-Xavier Cat
        EMAIL : fxcat@LazyWinAdmin.com
        DATE  : 2013/02/05 

        [string[]]$ComputerName = $env:COMPUTERNAME
    BEGIN {# Setup
        Foreach ($Computer in $ComputerName) {
            Write-Verbose -Message "ComputerName: $Computer - Getting Disk(s) information..."
            try {
                # Set all the parameters required for our query
                $params = @{'ComputerName'=$Computer;
                $TryIsOK = $True
                # Run the query against the current $Computer    
                $Disks = Get-WmiObject @params
            Catch {
                "$Computer" | Out-File -Append -FilePath c:\Errors.txt
                $TryIsOK = $False
            if ($TryIsOK) {
                Write-Verbose -Message "ComputerName: $Computer - Formating information for each disk(s)"
                foreach ($disk in $Disks) {
                    # Prepare the Information output
                    Write-Verbose -Message "ComputerName: $Computer - $($Disk.deviceid)"
                    $output =         @{'ComputerName'=$computer;
                                    'FreeSpace(GB)'=("{0:N2}" -f($disk.freespace/1GB));
                                    'Size(GB)'=("{0:N2}" -f($disk.size/1GB));
                                    'PercentFree'=("{0:P2}" -f(($disk.Freespace/1GB) / ($disk.Size/1GB)))}
                    # Create a new PowerShell object for the output
                    $object = New-Object -TypeName PSObject -Property $output
                    # Output the disk information
                    Write-Output -InputObject $object
                }#foreach ($disk in $disks)
            }#if ($TryIsOK)
        }#Foreach ($Computer in $ComputerName)
    END {# Cleanup
}#Function Get-DiskSizeInfo

Using the function:

By default, if you do not specify a computername, the function will query the localhost:  $env:computername

Getting some Help

Get-Help Get-DiskSizeInfo -Full
The Function's help has been wrote between the lines 2 and 39.
More information on technet: about_Comment_Based_Help

Use the Verbose switch

Get-DiskSizeInfo -ComputerName XAVIER -Verbose

The verbose switch is available since we use the [CmdletBinding()] at the line 41.
Check the nice article from the scripting guy: CmdletBinding Attribute Simplifies PowerShell Functions

Thanks for reading, hope this help some folks out there.


  1. Thanks for the post, great script! and good luck for the scripting games :-)

  2. Hi Francois-Xavier,

    How do you show the verbose in your script ?

    1. If you are using a function, like I did, all you need to do is add [CmdletBinding()]

      Check here for more information about the subject: