12 April,2017 by Tom Collins
Question: I need a powershell script to return drive letter , drive capacity , drive used and free space for a remote server on the network. Rather than checking on the Windows GUI - I want to run a powershell script from my laptop .
I've checked two other posts on sqlserver-dba.com but they are not exactly the format I need, The post Free disk space on drives using Powershell (SQL Server DBA) is focused on connecting to SQL Server and returning xp_fixeddrives details. And Powershell script – Function to return server disk capacity is almost what I need but the formatting of the output I'd prefer to be tabular and not as a function
Answer: I understand not one size fits all. The formatting is important , particularly if you need it as a quick reference and as an alternative to the Windows GUI .Using powershell wmi query , we can easily format the output into a tabular format. This code calls a remote server and returns the 4 columns in an easily readable format , with GB size format. Much easier than bytes.
Get-WmiObject Win32_logicaldisk -ComputerName SERVER123 ` | Format-Table DeviceID, ` @{Name="Drive Size(GB)";Expression={[decimal]("{0:N0}" -f($_.size/1gb))}}, ` @{Name="Drive Free Space(GB)";Expression={[decimal]("{0:N0}" -f($_.freespace/1gb))}}, ` @{Name="Drive Free pct";Expression={"{0,6:P0}" -f(($_.freespace/1gb) / ($_.size/1gb))}} ` -AutoSize ---------SAMPLE OUTPUT----------------
DeviceID Drive Size(GB) Drive Free Space(GB) Drive Free pct
-------- -------------- -------------------- --------------
C: 60 9 14 %
E: 2048 786 38 %
F: 400 256 64 %
G: 500 338 68 %
H: 3072 301 10 %
K: 5120 1030 20 %
Z: 5 1 17 %
If you like this script and are interested in using more Powershell in your DBA Daily Routine then check out these Powershell Scripts
This is only a preview. Your comment has not yet been posted.
As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.
Having trouble reading this image? View an alternate.
Posted by: |