One of the great things about Windows PowerShell, is that it allows us IT Administrators to write relatively simple, single-line commands to retrieve specific information about our servers and present it just the way we want. I will be posting my own PowerShell Oneliners frequently and explain how they work.
Check the size of type 3, logical volumes in GB, the free space in MB and calculate the percentage free space:
Get-WmiObject Win32_logicaldisk |
Where-Object {$_.drivetype -eq 3} |
Format-Table -Property Name,VolumeName,`
@{label=”Size (GB)”;expression={[math]::truncate($_.size/1GB)}},`
@{label=”Free Space (MB)”;expression={[math]::truncate($_.freespace/1MB)}},`
@{label=”Percent Free”;expression={[math]::truncate((($_.freespace/1GB)/($_.size/1GB))*100)}}
First, I use Windows Management Instrumentation (WMI) to get all information available about the logical disks in my computer.
The result is piped to Where-Object, which filters out the objects that represent a volume.
Then I specify the way I want the output to be formatted: which is in a table, with columns showing the Driveletter, Volume Name, Size, Free Space and Percent Free. I am using a trick here to do some calculations on the properties before I display them. You can use the format @{label=”label”;expression={expression}} to achieve this.
By the way: For readability I have truncated the command at the pipeline character and used the line break character ` (backtick) in several other places. You can however put the entire command in a single line and execute it from the PowerShell Command Prompt.
No related posts.
One Response to PowerShell Oneliner #1
Tags
Active Directory API bind order cleanup cluster CPU Custom Fields datastores description device management directory tree errors Event Log file name filter Fun function HA IT known issues License Server LUN multipath NIC objects Oneliner portgroups PowerCLI PowerShell profile recursive Registry Scripts security session share snapshots SQL Stat VI Toolkit VMware vSphere WMI WSUS ZenArchives
- July 2012
- July 2011
- February 2011
- January 2011
- December 2010
- May 2010
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008





Just testing the comments…