Powershell can do lots of things for us. To get disk size is one of them. This article is to tell how can we write Powershell function to print total disk size, how much is used and how much is free.
To get total disk size, we need to use get-disk
and get value of its property AllocatedSize
:
> (get-disk).AllocatedSize
512109142016
The return value is number of byte of the allocated disk space, to convert it to human-readable value (e.g. 100GB), we need to do some more work:
First, define variable for 1GB:
$one_gb = 1024*1024*1024;
Then, divide value of (get-disk).AllocatedSize
by $one_gb
:
$total_space = ((get-disk).AllocatedSize)/$one_gb;
Now, the result is:
> $one_gb = 1024*1024*1024;
> $total_space = ((get-disk).AllocatedSize)/$one_gb;
> $total_space
476,938804626465
As you see, it has 12 decimal after decimal point. How to cut it to less? We need to use static methodround
of Math
:
$total_space = [Math]::Round($total_space, 2)
Then, it’s much better:
> $total_space = [Math]::Round($total_space, 2)
> $total_space
476,94
Now, we only need to print the result number with the suffix “GB”:
> echo "Total: $total_space GB"
Total: 476.94 GB
After getting total disk size, we need to get how much has been used and how much is still free. How to do that? We can use get-psdrive
:
> get-psdrive | gmTypeName: System.Management.Automation.PSDriveInfoName MemberType Definition
---- ---------- ----------
CompareTo Method int CompareTo(System.Management.Automation.PSDriveInfo drive), int CompareTo(System.Object obj), int ICompara...
Equals Method bool Equals(System.Object obj), bool Equals(System.Management.Automation.PSDriveInfo drive)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Credential Property pscredential Credential {get;}
CurrentLocation Property string CurrentLocation {get;set;}
Description Property string Description {get;set;}
DisplayRoot Property string DisplayRoot {get;}
MaximumSize Property System.Nullable[long] MaximumSize {get;}
Name Property string Name {get;}
Provider Property System.Management.Automation.ProviderInfo Provider {get;}
Root Property string Root {get;}
Free ScriptProperty System.Object Free {get=## Ensure that this is a FileSystem drive...
Used ScriptProperty System.Object Used {get=## Ensure that this is a FileSystem drive...
By checking member of get-psdrive
we get to know it provides two properties Free
and Used
for free disk space and used disk space respectively.
We can access those two properties in below way:
> (get-psdrive).free
146537414656
> (get-psdrive).used
363919581184
If you have multiple drives, you can add drive label accordingly:
> (get-psdrive c).free
146302582784
> (get-psdrive c).used
364154413056
And you can get all drive names by:
> (Get-PSDrive -PSProvider FileSystem).name
C
And we can use the same way to convert the long number to be human-readable.
Now, we can put all code together to define one function to get total disk size, used and free disk size, and then print them together:
Maybe you noticed that I used two method to highlight the color of used and free space size value: to_yellow()
and to_green()
.
Here is how they are defined:
You can read my article “PowerShell: How to use Color in Prompt, Console, and Output Message” to know more about color usage in Powershell.
That’s all about how to get disk space size by powershell and how to define function to do it.
Thanks for reading and happy coding!