$computername = "Server1" $disks = Get-WmiObject -Class Win32_logicaldisk -ComputerName $computername | Where-Object { $_.DriveType -eq 3} $DriveC = $Disks | Where-Object -Property DeviceID -eq -Value 'C:' $DriveD = $Disks | Where-Object -Property DeviceID -eq -Value 'D:' $DriveE = $Disks | Where-Object -Property DeviceID -eq -Value 'E:' $DriveF = $Disks | Where-Object -Property DeviceID -eq -Value 'F:' $info = [PSCustomObject]@{ ServerName = $ComputerName 'C: (GB)' = "{0:n2}" -f (($DriveC | Select-Object -ExpandProperty Size) / 1GB) 'Free Space on C: (GB)' = "{0:n2}" -f (($DriveC | Select-Object -ExpandProperty FreeSpace) / 1GB) 'D: (GB)' = "{0:n2}" -f(($DriveD | Select-Object -ExpandProperty Size) / 1GB) 'Free Space on D: (GB)' = "{0:n2}" -f(($DriveD | Select-Object -ExpandProperty FreeSpace) / 1GB) 'E: (GB)' = $(if($DriveE -eq " ") { "{0:n2}" -f (($DriveE | Select-Object -ExpandProperty Size) / 1GB) } else { $DriveE = "N/A"}) } $info | Format-Table
I am trying to develop this powershell script to get disk space information from each drive on a server. Right now the script is for one server but later I will modify it to run for multiple servers.
Different servers have different drives, like some servers have only C and D drive. Whereas some servers have E and F drive or G drive also. So I want to mention "N/A" for servers which don't have that drive in the output. What is the best way to accomplish this?