Script to get the free space in differents hard disk units

Posted on 2012-03-11
Last Modified: 2012-06-03
Hi guys,

I need your help. I am not able to achive it by myself, I am trying but I am having troubles.

I need to run a script to get the size of a unit and the avilable free space. The same for all units in that server.  I would like to create an .xls with the output

server: server01

c:   20 gb   10 gb free
d:  30 gb    20 gb free
and so for all units

I would like to use WSH and WMI but it is not friendly for me as I would like. I am working hard to learn but I need now.

Could anyone help me?
Question by:projectja
  • 3
  • 3

Expert Comment

ID: 37707696
This script [Powershell] returns CSV file with DeviceID, Size and FreeSize of all local hard drives

Get-WMIObject Win32_LogicalDisk -Filter "DriveType=3" | Select-Object DeviceID,FreeSpace,Size | Export-Csv ./localhdd.csv

Author Comment

ID: 37713103

Thanks for the reply, It is interesting.

Please, let me explain better.

I would need a powershell such as:

- It will be launch from  my workstation or server of my domain providing administrattors credential in other domains to be able to execute command there.
- The goal is to get:
* To get information about differents hard disk units on target server; total space in disk and free space available. (could have c,d,e,...) units. There is not a rule.
* Export that information to excel, creating the file iin the pc where the scrilpt is being launching (source pc in which I am launching the script). The name of the file is the same that the IP adress of the server where we are connecting to get information.
* In a specific folder on the target server check for files which are  contained into a specific folder and are two weeks old, the goal is delete them (are logs files). report the action in a logs indicating amount of deleted files and the size disk.

Please, I need the help of an expert, It is very important for me to be able to finish this task.


Expert Comment

ID: 37713583
## This scripts generate 2 output files
## remoteip.csv - CSV file that includes all logical drivers of a computer with free space and total size
## remoteip.log - Log file that includes all files remove from remote folder that have more than 15 days (last write access)

# Constants

#$pathoutputfiles ALWAYS ends with \
$pathoutputfiles = "c:\path\to\output\files\"

# Read parameters

Write-Host "Enter remote computer IP (Format: ..."
$remoteip = Read-Host
Write-Host "Enter credentials for remote computer connection (Format: domain\username)..."
$credential = Get-Credential
Write-Host "Enter remote folder to scan (Format: \\server\path\) ..."
$remotefolder = Read-Host

# Get Hard Disk stats

Get-WMIObject Win32_LogicalDisk -Filter "DriveType=3" | Select-Object DeviceID,FreeSpace,Size | Export-Csv "$pathoutputfiles$remoteip.csv"

# Scan remote logs files
$stream = [System.IO.StreamWriter] "$pathoutputfiles$remoteip.log"
$files = Get-ChildItem -Path "$remotefolder" | Where-Object {$_.LastWriteTime -le [System.DateTime]::Now.AddDays(-15)}
foreach ($file in $files) {
    if ($File -ne $NULL) {
            Write-Host "Deleting File $File"
            $stream.WriteLine("Deleting File $File")
            Remove-Item $File.FullName | Out-Null
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)


Author Comment

ID: 37719491
Sorry for the dalay.

I have been working, but I happily saw your reply.
It is very intesting. Now, I am working to prepare it.
With regard to the .XLS file, I would like to add the day date so I have modified in this way:

$fechadeHoy = [System.DateTime]::Now

$month = $fechadehoy.month.ToString()
$year= $fechadehoy.year.Tostring()

$stream = [System.IO.StreamWriter] "$pathoutputfiles$remoteip$day$month$year.log"


- I get "month" = 3,  I want "03"

Any advice?

Accepted Solution

fundacionrts earned 500 total points
ID: 37719675
$month = $fechadehoy.ToString("MM")
$year= $fechadehoy.Tostring("yyyy")

Author Comment

ID: 37732371
Thank you very much. Is has been very useful

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question