PowerShell Script for Disk usage

Hey Guys,

I'm looking for a powershell script that returns the disk usage on a File Server mounted partitions.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rainer JeschorCommented:
a good starting point for such scripts is normally the Powershell script gallery.
Maybe this one fits your needs:

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
You need to be specific. "disk usage" is very broad.
Hani_SAAuthor Commented:
I meant a ps script that returns as output the current disk space usage on a specific partition which I intent to use as an alert for threshold monitoring.
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

Hani_SAAuthor Commented:
Hey Rainer,

I tried the script but couldn't get any result.
PS > $cred = Get-Credential
PS > Get-RemoteDiskInformation -ComputerName Test1 -Credential $cred

I'm trying locally on my laptop
by using
powershell -ExecutionPolicy ByPass -File .\Get-RemoteDiskInformation.ps1 -ComputerName MyLaptop $cred

Also is there a way to get rid of the unauthorized Access message
.\Get-RemoteDiskInformation.ps1 cannot be loaded because running scripts is disabled on this system.
and integrate the permission within the script

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
You should execute set-executionPolicy RemoteSigned once. Then you should be able to execute the script without prompt, as is with ByPass on PowerShell invokation as shown above.

You are not getting results because the script just defines the function, but does not execute it. And the parameters you provide might be consumed by PowerShell, not the command itself, as you do it. You need to import the function by dot-sourcing the script, and then call the function. That is:
PowerShell -ExecutionPolicy Bypass -Command { . .\Get-RemoteDiskInformation.ps1; Get-RemoteDiskInformation -ComputerName MyLaptop }
# or, if you need credentials:
PowerShell -ExecutionPolicy Bypass -Command { . .\Get-RemoteDiskInformation.ps1; Get-RemoteDiskInformation -ComputerName MyLaptop -credential (Get-Credential MyUser) }

Open in new window

Hani_SAAuthor Commented:
Thanks Qlemo,

It worked as suggested so what is the purpose of set-executionPolicy RemoteSigned  if I need to use PowerShell -ExecutionPolicy Bypass every time.
You see my intention is use this script as input from a monitoring tool that will call on this script to run so I can only only provide the script name in the tool without arguments, do you think there is a way to integrate this bypass command in the script itself.
In addition the script provides an extensive amount of info, is there a way to fine-tuned in a way to get only:

where Drive is to be defined in the script as a static value since I'm only interested in monitoring 1 critical partition on my file server let say for example v:\

Thanks for your help
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
You don't need to provide the execution policy when calling PowerShell if you set it correctly with set-executionpolicy, only to override it.
Hani_SAAuthor Commented:
Does this setting be saved after logoff/logon.
Can you explain the intention of this command:
Command { . .\Get-RemoteDiskInformation.ps1;
you mentioned that it's related to an import !
Also I found this script :

Get-WmiObject -ClassName Win32_Volume -Filter "DriveLetter = 'C:'"-ComputerName localhost   |

select PSComputerName,Caption,Label,
@{N='Capacity_GB'; E={[math]::Round(($_.capacity / 1GB), 2)}},

@{N='FreeSpace_GB'; E={[math]::Round(($_.Freespace / 1GB), 2)}},

@{N='PercentUsed'; E={[math]::Round(((($_.capacity - $_.FreeSpace) / $_.capacity) * 100), 2) }},

@{N='PercentFree'; E={[math]::Round((($_.FreeSpace / $_.capacity) * 100), 2) }}

is there a way to monitor a specified path under c:\ instead of the entire volume i.e C:\TestFolder in order to get the above values for this specified partition
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
The default scope of Set-ExecutionPolicy is LocalMachine, so yes, it is persistent.

The need for "importing" (it is called dot-sourcing in PS) is kind of complex to explain. A script builds an own scope, and everything done in the script does not effect the calling environment. This includes function and variable definitions. Only the objects generated as output (like the disk info here) are returned and stay valid after the script has executed.
However, if you run . .\scriptname.ps1 all definitions of the script are "imported" into the current shell environment, and can hene be used. This includes the function defined in the script.

The script you found can be used if have drive letters. Mount points are not necessarily drives, but can also be paths. So it depends on what you really want to know. I guess in your case the new script will work fine, and it doesn't matter which one you'll use.
But you can take the original approach. Just put together a "master script" doing the work:
 . .\Get-RemoteDiskInformation.ps1
Get-RemoteDiskInformation -ComputerName MyLaptop |
  ? { $_.Drive -eq 'V:' } |
  select Drive, DiskSize, UsedSpace, PercentFree, PercentUsed

Open in new window

Regarding your new request - you told us you just want to monitor a single, specific volume (V:). Now you take a twist to monitor a path. The answer is "no, you can't do that" with that WMI call, because getting folder stats is something completely different than to get volume or drive stats. Aside from that, the only useful info from above you can restrict to a path is "PercentUsed", anything else remains a volume number.
Hani_SAAuthor Commented:
Thanks Qlemo for your valuable info.

It works perfectly well except for the import part; I received the below error so I had to use PowerShell -ExecutionPolicy Bypass -Command once again:
Get-RemoteDiskInformation.ps1 cannot be loaded. The file is not digitally signed.

Is there a way to bypass that ?

Also regarding the path monitor which I requested at a later stage, I found a class to do that, do you think it achieve that purpose that said let's say I want to monitor a specific mount point path on my file server i.e v:\shared\business and not the entire v:\ mount point

'{0:0.00}' -f ((Get-Childitem -path v:\shared\business -recurse | Measure-Object length -sum).Sum/1GB)

Do you agree in case I integrate this ps in my monitoring tool, will it serve the intent of monitoring a threshold let's say below 10 % for the specified path

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
That latest script fragment should work to get the used space.
But be aware that it is a costly operation. Get-ChildItem has to retrieve the complete FileObject or FolderObject with many of its attributes, and doing that remote might cause some traffic. If you can do so, perform the action locally, where the folder you want to monitor is located at.

Having said that, what's the 10% based on? Are you setting an arbitrary limit for used space, say 100 MB, and if the real used space is above 90 MB (less than 10% "free") trigger an alert?
Hani_SAAuthor Commented:
I'm planning to run this command remotely, do you have any alternative ?
My end result is to have an event on my monitoring tool triggered once the disk utilization threshold on this particular path reaches 80% usage, how to achieve that ?
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
80% of which size?
Hani_SAAuthor Commented:
let say this is the path I wish to monitor:
it has a 100 GB size
Can you provide a script to return the size of this path and alerts me whenever usage is above 80% (80 GB)

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
How should it alert you? For startes, here is the simple approach getting you current size, limit, free (based on limit), and free% (based on limit):
$limit = 100 # GB
$usedGB = (Get-Childitem -path v:\shared\business -recurse | Measure-Object length -sum | select -Expand Sum)/1GB
New-Object -PsObject -Property @{
  used = $usedGB
  free  = $(if ($limit -gt $used) {$limit - $used} else {0})
  freePercent = ($limit - $used) / $limit * 100.0

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Hani_SAAuthor Commented:
Dear Qlemo,

Thank you very much for your help, I fine-tuned the script and now it looks like this
$limit = 5 # GB
$usedGB = '{0:0.00}' -f ((Get-Childitem -path C:\Test  -recurse | Measure-Object length -sum | select -Expand Sum)/1GB)
New-Object PsObject -Property @{
  used = $usedGB
  free  = $(if ($limit -gt $usedGB) {$limit - $usedGB} else {0})
  freePercent = ($limit - $usedGB) / $limit * 100.0


Open in new window

If you can please explain this New-Object PsObject -Property @{
However I run into another issue since the server on which I intend to run the command has powershell 1.0 and another issue is that the shared disk I am monitoring has over 400 GB space which could cause a performance issue since it will take a lot of time to traverse the path not to mention the calculation method which could not be accurate.
What do you propose to overcome those issues ?
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
The New-Object line creates a new custom object with the properties as supplied. In PS, everything is an object.
With PS1, you had to use Add-Member (or more complicated methods) to add properties to an object.

It is very unlikely you have PS1 installed, even if the path of PowerShell.exe suggests so. There is no reason to have PS1, it can always be upgraded to PS2.

I don't know of a better way to calculate folder specific stats. All I can say is that I would run a job on the server machine itself performing the check.
Have a look at OvaFlow.com also.

Instead of scripting all this it does all this under the bonnet. You can monitor up to 100 servers and takes about 15 minutes to install. All totally free as well.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.