Using Windows Power Shell to get the Server Disk Space Information

To find Drive Names, Total Space, Free Space and Free Space Percentage for the list of Servers specified in computers.txt file using Power Shell Script.

Prerequisites:-

  1. Save the computers.txt file and below PS script in the same folder
  2. User should have access to the servers mentioned in computers.txt file
  3. There should not be any empty space or new line without Servername in computers.txt file

PS Script:-

$path=Split-Path $MyInvocation.MyCommand.path 
$Computers = get-content "$path\computers.txt" 
foreach ($Computer in $Computers)  
{  
$Disks = Get-wmiobject  Win32_LogicalDisk -computername $Computer -ErrorAction SilentlyContinue -filter "DriveType= 3" 
$Servername = (Get-wmiobject  CIM_ComputerSystem -ComputerName $computer).Name
foreach ($objdisk in $Disks)  
{  
        
    $total=“{0:N3}” -f ($objDisk.Size/1GB)  
    $free= “{0:N3}” -f ($objDisk.FreeSpace/1GB)  
    $freePercent="{0:P2}" -f ([double]$objDisk.FreeSpace/[double]$objDisk.Size)  
       
       Write-Host "Servername      :" $Servername  
       Write-Host "Drive Folder    :" $objDisk.DeviceID "Drive" 
       Write-Host "Total size (GB) :" $total 
       Write-Host "Free Space (GB) :" $free 
       Write-Host “Free Space (%)  :” $freePercent 
} 
} 

Output:-

To save the Output to Diskspace_Report.csv file, Use below PS script:-

$path=Split-Path $MyInvocation.MyCommand.path 
$Computers = get-content "$path\computers.txt" 
foreach ($Computer in $Computers)  
{  
$Disks = Get-wmiobject  Win32_LogicalDisk -computername $Computer -ErrorAction SilentlyContinue -filter "DriveType= 3" 
$Servername = (Get-wmiobject  CIM_ComputerSystem -ComputerName $computer).Name
foreach ($objdisk in $Disks)  
{  
        
    $total=“{0:N3}” -f ($objDisk.Size/1GB)  
    $free= “{0:N3}” -f ($objDisk.FreeSpace/1GB)  
    $freePercent="{0:P2}" -f ([double]$objDisk.FreeSpace/[double]$objDisk.Size)  

        $out=New-Object PSObject 
        $out | Add-Member -MemberType NoteProperty -Name "Servername" -Value $Servername 
        $out | Add-Member -MemberType NoteProperty -Name "Drive" -Value $objDisk.DeviceID  
        $out | Add-Member -MemberType NoteProperty -Name "Total size (GB)" -Value $total 
        $out | Add-Member -MemberType NoteProperty -Name “Free Space (GB)” -Value $free 
        $out | Add-Member -MemberType NoteProperty -Name “Free Space (%)” -Value $freePercent 
        $out | Add-Member -MemberType NoteProperty -Name "Name " -Value $objdisk.volumename 
        $out | Add-Member -MemberType NoteProperty -Name "DriveType" -Value $objdisk.DriveType 
        $out | export-csv $path\Diskspace_Report.csv -NoTypeInformation -Append   
} 
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s