Get Drive Size

Powershell Function to Get Drive Size

Checking drive size, free space and occupied space for local server/computer  is very easy, you can check through windows explorer, disk management and number of other ways but checking the same thing for remote server is difficult.

Here is a script which you can use to get drive size for local server/computer as well for remote server/computer. This script is having a function once executed a new cmdlet will be added in powershell.

How to use:  Open notepad then copy below script text and paste it in notepad, Save notepad file on the name of customfunction and extension .ps1 a powershell script file. make sure notepad file is not saved as a .txt file extension, if it is then it will not execute.

Open Powershell and Run command (Get-Executionpolicy) to check execution policy of the system where this script need to be executed. if it is unrestricted then you can execute the same or else need to change execution policy with the powershell command (Set-Executionpolicy unrestricted) , A warning message will come press y to set the execution policy.

in powershell go to the location where the script is saved and execute the same by typing .\customfunction.ps1

Now powershell cmdlet is ready to use for all local/remote servers/computers.

Get-DriveSize localhost: This powershell command will list all system drives, Free space, Drive Size and %Free Space, to list the size of remote system just provide remote system name instead of localhost.

Get-Drive Size

Get-DriveSize localhost c,d: This powershell command will list only c and d drive, free space, drive size and %free space.
function get-DriveSize{
param(
[parameter(mandatory = $true)]
$server,
$drive
)
if ($server -eq “”)
{
Write-Host “Please provide server name”
}
elseif($drive -eq $null)
{
gwmi win32_logicaldisk -computername $server | where-object {$_.drivetype -eq “3”} | format-table @{label=‘Server Name’; expression={$server}}, Deviceid, @{ label=‘Free space’; expression={$_.freespace/1gb}; f=“{0:N2} GB”},@{label=‘Drive Size’; expression={$_.size/1gb}; f=“{0:N2} GB”}, @{label=‘%Free Space’; expression={$_.freespace * 100/$_.size}; f=“{0:N2}”} -auto
}
else
{
$arr = @()
for($i=0; $i -lt $drive.Length; $i++)
{
$arr += ‘$_.deviceid -match ‘ + ‘”‘ + $drive[$i] + ‘”‘
}
$jn = $Arr -join ” -or “
$exp = ‘gwmi win32_logicaldisk -computername $server | where-object { ‘ + $jn + ‘} | format-table @{label=”Server Name”; expression={$server}}, Deviceid, @{ label=”Free space”; expression={$_.freespace/1gb}; f=”{0:N2} GB”},@{label=”Drive Size”; expression={$_.size/1gb}; f=”{0:N2} GB”}, @{label=”%Free Space”; expression={$_.freespace * 100/$_.size}; f=”{0:N2}”} -auto’
Invoke-Expression $exp
}

Disk Usage Percentage

Power-shell Script to show Disk Usage Percentage.

Disk Usage in Percentage  – A Script to show the usage of system disk in graphical charts. you can copy this script to use in your system Similar kind of script uploaded earlier, the main difference for this script is it will show the disk usage percentage wise so we could identify how much the space is occupied in terms of percent.

This script will load Assemblies and windows form provided by .Net Framework.

How to Use.
Copy below script text to a file called “DiskUsage.ps1″
Run Powershell go to the path where file is saved.
Run script DiskUsage.ps1 for local system
Run script DiskUsage.ps1 x.x.x.x for remote system

Requirement: Powershell v2.0 or later, .Net Framework 4.0 or later

#——————————————————————
# load the appropriate assemblies
[void][Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
[void][Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms.DataVisualization”)
[void][Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)
[void][Windows.Forms.Application]::EnableVisualStyles()
[void][Windows.Forms.ComboBoxStyle]::DropDown
$Form = New-Object Windows.Forms.Form
$Form.Text = “PowerShell Chart”
$Form.Width = 600
$Form.Height = 600
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Font = New-Object System.Drawing.Font(“Cooper Black”, 14.0, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
$Label1.Location = New-Object System.Drawing.Point(40, 5)
$Label1.Size = New-Object System.Drawing.Size(350, 50)
$Label1.TabIndex = 1
$Label1.Text = “Disk Usage in Percentage”
$Label1.BackColor = [System.Drawing.Color]::Transparent
$Label1.ForeColor = [System.Drawing.Color]::DarkRed
$Chart = New-object System.Windows.Forms.DataVisualization.Charting.Chart
$Chart.Width = 500
$Chart.Height = 500
$Chart.Left = 40
$Chart.Top = 30
$ChartArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea
$chartarea.AxisY.Interval = 5
$chartarea.AxisY.Maximum = 100
$Chart.ChartAreas.Add($ChartArea)
[void] $Chart.Series.Add(“Data”)
if (!$args)
{
$Usage_in_Percentage = gwmi win32_logicaldisk -Filter “drivetype=3″ | select deviceid,@{n=’Usage’; e={{0:n2} -f ((($_.size/1gb) – ($_.freespace/1gb)) * 100)/($_.size/1gb) }}
$t = $Usage_in_Percentage | ForEach-Object { $chart.Series[“
Data”].Points.AddXY($_.deviceid,$_.usage) }
}
else
{
$Usage_in_Percentage = gwmi win32_logicaldisk -ComputerName $args -Filter “drivetype=3″
| select deviceid,@{n=’Usage’; e={“{0:n2}” -f ((($_.size/1gb) ($_.freespace/1gb)) * 100)/($_.size/1gb) }}
$t = $Usage_in_Percentage | ForEach-Object { $chart.Series[“Data”].Points.AddXY($_.deviceid,$_.usage) }
}
$Chart.Series[“Data”].ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::column
$Form.controls.add($Chart)
$Form.Controls.Add($Label1)
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()
#——————————————————————
Script Output

Disk Usage in Percentage