Click here to Skip to main content
15,881,172 members
Articles / All Topics
Technical Blog

Retrieve Disk Space of Remote Computers using PowerShell

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
10 Apr 2014CPOL2 min read 59.7K   6   4
Retrieve Disk Space of Remote Computers using PowerShell

The more I use PowerShell, the more I like it. I expect if you are reading this blog right now, then you are already aware of What PowerShell is.

For those who are new to this topic, Windows PowerShell is Microsoft’s task automation and configuration management framework, consisting of a command-line shell and associated scripting language built on .NET Framework. Although many consider it as an extention of good old command prompt (CMD), it's much more extendable scripting language. It’s simply cool because as a developer you can actually write all those small toys you were writing as console applications or executable, isn’t required any more. One can have interaction with all your .NET libraries using PowerShell itself. In fact, you can use COM too using p/Invoke.

Anyway without dragging the topic in PowerShell details, this tip/note-to-self is about how to get disk (free) space of a remote computer using PowerShell.

I wanted to know the total disk space and the available free space of number of servers. You can always use the RDP to log-in and check the details, but I wanted to avoid that step and have the insights remotely.

If you are looking for something similar, the following one line PowerShell is a good place to start:

Get-WmiObject Win32_LogicalDisk -ComputerName $ServerName -Filter "DeviceID='C:'" | Select-Object Size,FreeSpace

WMI remains the primary automation technology for system administration, so system administrators will likely rely heavily on Get-WmiObject to help them with their routine management tasks. And there’s an added bonus, too: unlike most cmdlets, Get-WmiObject can be run against remote computers. That means you really can use Windows PowerShell as a management tool.

So, now I was able to get the total Size and FreeSpace of the drive C of a particular server by passing the ComputerName. Next step was to retrieve the same details for an array of computers or servers.

In the below code snippet, I read a file named Computers.txt which has a list of server names listed. Thus, in just few lines of code/script, you can retrieve the details of the drive of remote computers. Isn’t it quite handy when you are doing lot of system management on a day to day basis?

You can format the details as per your requirements. In the following example, the output is generated as the comma separated records with Server Name, Total Disk space and Free Disk space available (in Giga Bytes).

$ServerName = Get-Content "C:\Users\Manas\Desktop\Computers.txt"
$ConvertToGB = (1024 * 1024 * 1024)

foreach ($Server in $ServerName) {

    $disk = Get-WmiObject Win32_LogicalDisk -ComputerName $Server -Filter "DeviceID='C:'" | Select-Object Size,FreeSpace
    $Server + "," + ($disk.Size / $ConvertToGB) + "," + ($disk.FreeSpace / $ConvertToGB)
}

Further Reference

The post Retrieve disk space of remote computers using PowerShell appeared first on Manas Bhardwaj's Stream.

This article was originally posted at http://manasbhardwaj.net?p=486

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Netherlands Netherlands

Read my personal blog at www.manasbhardwaj.net.


Comments and Discussions

 
QuestionDisplay all properties Pin
Member 1164602627-Apr-15 5:52
Member 1164602627-Apr-15 5:52 
AnswerRe: Display all properties Pin
Manas Bhardwaj27-Apr-15 7:11
professionalManas Bhardwaj27-Apr-15 7:11 
QuestionTo bring disk on remote machine online Pin
Ganesh Satpute13-Apr-15 22:35
Ganesh Satpute13-Apr-15 22:35 
QuestionThanks Pin
ungert5812-Sep-14 6:58
ungert5812-Sep-14 6:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.