Prof. Powershell

PowerShell PSDrive In

Here are a few tricks that you can use in conjunction with a PSDrive.

Last time I tried to explain the concept of a Provider in PowerShell and why it might matter to you. The provider exposes its underlying technology through a PSDrive. By default, PowerShell defines a number of PSDrives.

[Click on image for larger view.] 

You should see a drive for each hard drive plus a few for some of the other default providers. One thing to be aware of is that the PSDrive name is without the colon. You only need the colon when you use the PSDrive.

[Click on image for larger view.] 

But there are some interesting tricks you can use with these PSDrives. Did you notice that the file system drives give you disk utilization? Why struggle with WMI when you can do this?

PS C:\> get-psdrive -PSProvider filesystem | select Root,Used*,Free*

Root                                 Used                                    Free
----                                 ----                                    ----
C:\                            70436589568                            144309673984
D:\                           437202550784                             32433664000
E:\                            16469356544                             10374184960

The values are formatted as bytes but that is easily handled and in fact you can go even further

PS C:\> PS C:\> get-psdrive -PSProvider filesystem | select Root, @{Name="UsedGB";Expression={[math]::round($_.used/1gb,2)}}, @{Name="FreeGB";Expression={[math]::round($_.free/1gb,2)}}, @{Name="PctFree";expression={$_.free/($_.free+$_.used)*100 –as [int]}}

Root    UsedGB      FreeGB              PctFree
----    ------      ------              -------
C:\       65.6       134.4                   67
D:\     407.18       30.21                    7
E:\      15.34        9.66                   39

It is also easy to add a new PSDrive, which I often do in the file system so that my prompt can be short. For example, I have a Dropbox folder I use for my demos, C:\users\jeff\Dropbox\Demo. So instead of navigating to it, I create a new "drive."

PS C:\> new-psdrive -PSProvider FileSystem -Name Demo -Root C:\users\jeff\Dropbox\Demo

Name    Used (GB)     Free (GB) Provider      Root                   CurrentLocation
----    ---------     --------- --------      ----                    --------------
Demo                     134.39 FileSystem    C:\users\jeff\Dropbox\Demo

You are not limited to single letters. Now I can change to my Demo: drive. This "mapping" only persists for as long as my PowerShell session is running so I put this in my PowerShell profile script to always have it.

One last trick I use, also in my profile is to set a default location. Notice that CurrentLocation property? You can modify it.

PS C:\> (get-psdrive hklm).CurrentLocation = "\SOFTWARE\Microsoft"

By default, the default location of HKLM: is the root. But now I've changed it. The location value is relative to the root. But now when I change locations to the HKLM PSdrive, I am automatically placed in the location.

PS C:\> cd hklm:
PS HKLM:\SOFTWARE\Microsoft

In my profile I use this to set a default location for my C: drive to my scripts directory.

(get-psdrive c).CurrentLocation = "Scripts"

Given that you can create new filesystem drives, let me leave you with a revised usage command that will ignore drives like my Demo: PSDrive.

PS C:\> get-psdrive -PSProvider filesystem | Where {$_.used } |select Root,@{Name="UsedGB";Expression={[math]::round($_.used/1gb,2)}}, @{Name="FreeGB";Expression={[math]::round($_.free/1gb,2)}}, @{Name="SizeGB";expression={($_.free+$_.used)/1gb -as [int]}}, @{Name="PctFree";expression={$_.free/($_.free+$_.used)*100 -as [int]}}

Root    : C:\
UsedGB  : 65.6
FreeGB  : 134.39
SizeGB  : 200
PctFree : 67

Root    : D:\
UsedGB  : 407.18
FreeGB  : 30.21
SizeGB  : 437
PctFree : 7

Root    : E:\
UsedGB  : 15.34
FreeGB  : 9.66
SizeGB  : 25
PctFree : 39

PSDrives are you way in to areas of Windows management information that you might never have thought about before.

About the Author

Jeffery Hicks is a multi-year Microsoft MVP in Windows PowerShell, Microsoft Certified Professional and an IT veteran with almost 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff is a regular contributor to a variety on online sites, as well as frequent speaker at technology conferences and user groups. Keep up with Jeff and his projects at http://jdhitsolutions.com/blog.

comments powered by Disqus

MCPMag.com

Sign up for our newsletter.

I agree to this site's Privacy Policy.