Get-DiskSizes, Including mount points using powershell


As a DBA, we are in the need of finding out how much free space we have on a drive or mount point for various reasons. Some of them being:

  • Validating if you have enough space to restore a DB
  • Making an educated guess if a backup will fit on the device
  • Creating an alert if the free space is below a certain threshold
  • Etc..

There are a lot of scripts, that get this information for us. So, here is one more PowerShell function that gets this information for us. It’s not new but, it is something I use often.

Function Get-DiskSizes
{
    param(
  [ValidateNotNullOrEmpty()]
        [String]
  $ComputerName
    )
  $wmiVolParams=@{
  'ComputerName' = $ComputerName;
        'Class'               = 'Win32_Volume';
        'NameSpace'           = 'ROOT\cimv2';
  'Authentication' = 'PacketPrivacy';
        'Impersonation'       = 'Impersonate';
  'EnableAllPrivileges' = 1
    }
  Get-WmiObject @wmiVolParams | Where-Object {$_.FileSystem -imatch 'ntfs'}|
  Select-Object -Property Name, @{Name='Size (GB)'; Expression = {'{0:N0}' -f ($_.capacity/1GB)}},
  @{Name='Free (GB)'; Expression = {'{0:N0}' -f ($_.freespace/1GB)}}|
  Sort-Object -Property Name
}
Advertisements
About

By profession, I’m a SQL Server Database Administrator. I love to poke my nose into different corners and see how stuff looks in there. I keep looking for new things to do as my mind refuses to settle on one topic.

Tagged with: , ,
Posted in PowerShell
2 comments on “Get-DiskSizes, Including mount points using powershell
  1. SM says:

    Hello Mr Hemanth SQL Chao.

    As usual this is also your best script. Good one.
    Adding one line for the “users” :

    To call the function, use as below:
    Get-DiskSizes “server-name”

    Cheers
    SM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: