Retrieve Disk space of Remote Computers using Powershell

0

In this article, we will be learning how to retrieve disk space of remote computers using Powershell. It is going to be interesting and you will be surprised how easy the administration task is done by Powershell.

The prerequisite is that Powershell Remoting needs to be turned on the remote computers so that we can execute the commands successfully on them.

The way you would do that is open Powershell with elevated permissions and type Enable-PSRemoting and say Yes to All.

That sounds easy, imagine doing that on hundreds of computers. Do not worry, go here and check this article, which will help you to turn the setting ON on all computers using Group Policy.

Now that we have the prerequisite taken care, lets move on to the meat of the article.

We can create a small little script which can be used whenever the need arises or schedule this run from Task scheduler.

Invoke-Command -ComputerName ED-EX2K10-CAH, ED-EX2K10-MB1, CLIENT01 -ScriptBlock {Get-WmiObject -Class Win32_logicalDisk -Filter "DeviceID='C:'" | Select SystemName, DeviceID, @{n='Size(GB)';e={$_.size / 1gb -as [int]}}, @{n='Free(GB)';e={$_.Freespace / 1gb -as [int]}}} > C:\DiskInfo_output.txt

Now let us analyze what we are doing here.

First of all, I have used a cmdlet called Invoke-Command, which is used to run any command that you want on a remote computer.

You can get more info with the help files by typing Get-Help Invoke-Command. It has a mandatory parameter which is the ComputerName. In our case we trying to run the commands remotely on three different machines.

The set of commands that you want to run on a remote computer have to given after the ScriptBlock parameter as you can easily see that from the example.

Next we will be using WMI objects to gather the information on the remote computers. Hence the next part of the code is Get-WMIObject and the class that we are trying to target is Win32_logicalDisk and the drive we are interested is C drive, hence we give the filter parameter.

After that we will pipe the output and select only what we want to show on the screen or capture in a file.

You can see the associate properties by piping the output to Get-Member command.

Get-WmiObject -Class Win32_logicalDisk -Filter "DeviceID='C:'" | Get-Member

Retrieve Disk space of Remote Computers using Powershell We will be selecting SystemName, DeviceID, size and FreeSpace

@{n='Size(GB)';e={$_.size / 1gb -as [int]}} We are making this easy to read and making custom output. We are just dividing the size of the disk with 1GB and storing that datatype as integer.

Similarly, we will be doing the same for Freespace as well.

We will save the file as Diskinfo.ps1. Let us run the ps1 from the Powershell and you should receive a similar output as below:

Note: You will have to change the Execution policy on your system to Remotesigned.

Set-ExecutionPolicy Remotesigned

You can check your current policy with Get-ExecutionPolicy

Retrieve Disk space of Remote Computers using Powershell

At the end, we will output the details to a text file named Diskinfo_output.txt.

I hope this was informative and thank you for reading!

Share.

About Author

I am Adil Arif, working as a Senior Technical Support Engineer at Rubrik as well as an independent blogger and founder of Enterprise Daddy. In my current role, I am supporting infrastructure related to Windows and VMware datacenters.

Leave A Reply