Use PowerShell to check logical disk information remotely

Today I want to show how easily you can get information about logical disk from remote machines using PowerShell. Below function will help to gather several useful information about logical disks. You can specify disk partition as a parameter from ValidateSet:

        [Parameter(Position=1, Mandatory = $true, HelpMessage="Select disk partition(C, D, E)", ValueFromPipeline = $true)][ValidateSet("C", "D", "E")][string]
        $Disk = "C:"

Function is based on Get-CimInstance command.

CimInstance
CimInstance

However we can get basic disk information also using Get-WmiObject:

Get-WmiObject -Query "Select DeviceID,Size,Freespace from Win32_logicaldisk where drivetype=3"
LogicalDisk
LogicalDisk

Our function will get all needed information and add them into array $Results:

– Computername
– DiskName
– DiskFree
– PartitionSize(GB)
– Model
– Firmware
– SerialNumber
– Partitions
– Partition
– BootPartition
– Blocks
– BlockSize
– FileSystem

Output:

Get-DiskInfo
Get-DiskInfo

Usage:

Get-DiskInfo -Disk C
Get-DiskInfo -Computername DC01 -Disk D
Get-DiskInfo -Computername DC01,DC02,DC03,DC04 -Disk D | Out-GridView
Get-DiskInfo -Computername (gc "c:\scripts\servers.txt") -Disk C | Export-Csv -Path C:\users\$env:username\desktop\diskresults.csv -NoTypeInformation

Final script:


  Function Get-DiskInfo {
    [CmdletBinding()]
              
    # Parameters used in this function
    Param
    (
        [Parameter(Position=0, Mandatory = $false, HelpMessage="Provide server names", ValueFromPipeline = $true)] 
        $Computername = "localhost",

        [Parameter(Position=1, Mandatory = $true, HelpMessage="Select disk partition(C, D, E)", ValueFromPipeline = $true)][ValidateSet("C", "D", "E")][string]
        $Disk = "C:"
    ) 
      
    $ErrorActionPreference = "Stop"
    $Results = @()

    Switch ($Disk)
    {
        "C"     { $Val = "C:" }
        "D"     { $Val = "D:" }
        "E"     { $Val = "E:" }
    }

    ForEach($Comp in $Computername)
    {
        Try
        {    
            $DiskInfo = Get-CimInstance -ComputerName $Comp -ClassName Win32_LogicalDisk -Filter "DriveType = 3" | Where-Object {$_.DeviceID -eq "$Val"}
            $Partition = Get-CimAssociatedInstance -ComputerName $Comp -InputObject $DiskInfo -ResultClass Win32_DiskPartition
            $DiskDrive = Get-CimAssociatedInstance -ComputerName $Comp -InputObject $Partition -ResultClassName Win32_DiskDrive
        }
        Catch
        {
            $_.Exception.Message
            Continue
        }
            If($DiskInfo -and $Partition -and $DiskDrive)
            {
                $Object = New-Object PSObject -Property ([ordered]@{ 

                    Computername               = $Comp
                    DiskName                   = $DiskInfo.Caption
                    DiskFree                  =  [math]::Round(($DiskInfo.FreeSpace / 1GB ), 2)
                    'PartitionSize(GB)'        = [math]::Round(($Partition.Size / 1GB ), 2)
                    Model                      = $DiskDrive.Model
                    Firmware                   = $DiskDrive.FirmwareRevision
                    SerialNumber               = $DiskDrive.SerialNumber
                    Partitions                 = $DiskDrive.Partitions
                    Partition                  = $Partition.index                  
                    BootPartition              = $Partition.BootPartition
                    Blocks                     = $Partition.NumberOfBlocks
                    BlockSize                  = $Partition.BlockSize                   
                    FileSystem                 = $DiskInfo.FileSystem

                   })
  
                   $Results += $Object
            }
    }

    If($Results)
    {
        $Results
    }
}


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.