3 methods to get the drive sizes with Powershell

19 February,2016 by Jack Vamvas

Retrieving the drive sizes from a server is common administration task. Like many administration task there are loads of different methods to use. Powershell is continiously evolving. One of it's great strengths is the ability to read straight from server APIs , that are are built specifically with Powershell in mind, allowing Powershell to return values with minimal need to manipulate strings.

Powershell also does a great job at allowing existing methods to be utilised using the Powrershell scripting language to manage the workflow.

This is a quick summary of 3 methods I use across different SQL Server management situations. These are starting points

 

Method 1 : Using the Get-OSDrive cmdlet . It will return drives that are in the current session

Get-PSDrive C | Select-Object Used,Free

Method 2 : Using the Get-WmiObject. There is the added bonus of accessing a remote server via the -computer switch.

 

Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -computer MyComputer | Select SystemName,DeviceID,VolumeName,@{Name="Size(GB)";Expression={"{0:N1}" -f
($_.size/1gb)}},@{Name="FreeSpace(GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}} | Out-GridView

Method 3 : SQLCMD invoked by Powershell. The benefit is you can use the SQL Server based commands via the Powershell framework

 

invoke-expression "SQLCMD -E -S server\myinstance; -q 'exec master.dbo.xp_fixeddrives'"

 

Read More on Powershell

Expand your Powershell mind – Three key cmdlets - SQL Server DBA

Powershell Scripts for DBA

I want to become a DBA. What should I first learn ?

 

 

 

 


Author: Jack Vamvas (http://www.sqlserver-dba.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on 3 methods to get the drive sizes with Powershell


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer