So I ended up stuck in an odd situation the other day. I needed to know how large someones hard drive was, however I didn’t have access to the computer and it wasn’t listed in the asset management software that I use. I do know WMI is enabled on all domain computers, I also decided Powershell was the quickest way to obtain the harddrive size, without diving into VB, SNMP or any other number of ways to find the same information.
Using this article from stack overflow I put together a step by step that will help me in a pinch in the future. There are scripts that can do this as well, however, I didn’t feel the need for a script. This also neatly finds the harddrive size without a lot of fuss. 99% of the time, my asset management software would do this for me.
How to Obtain Harddrive Size using Powershell
- Open up powershell as an administrator. Either on a server or your local windows 7/8 pc.
- Next we will assign the object to a variable so we can use it later. I use the variable $disk. At the prompt
$disk = Get-WmiObject Win32_LogicalDisk -ComputerName computername -Credential domain\adminaccount -filter “DeviceID=’C:'”
Get-WmiObject Get WMI class information, instances of classes or available classes. Alias: gwmi
Win32_LogicalDisk The Win32_LogicalDisk WMI class represents a data source that resolves to an actual local storage device on a computer system running Windows.
ComputerName Simply input the computer name you wish to access
Credential Passes the domain or local credentials to the query. Since I was operating within a domain, it will immediately ask for the domain password based on the credentials you put into the above line.
Filter Allows me to filter the output to retrieve what I want. Remove -Filter if you want to see the scope of information you can retrieve. Since I only wanted to see the information for C drive. That is what I filtered for.
There is no output from this command.
- Next step is to simply pull the information from the object we want, at the same time we will do some math to display the size in GB.
In my test case is 19gb, at this stage I don’t care about free space, but I could easily query that as well.
This output show my freespace at 5gb.
Here is the entire result from beginning to end in my effort to obtain harddrive sizes remotely using Powershell.
PS C:\ $disk = Get-WmiObject Win32_LogicalDisk -ComputerName ComputerName -Credential domain\adminaccount -filter “DeviceID=’C:'”
PS C:\ $disk.size/1gb
PS C:\ $disk.free/1gb
There are a number of items you can access using the WMI class through Powershell.