PowerShell Get-PSDrive

Windows PowerShell’s Get-PSDrive

This cmdlet is not only useful for researching the local disk structure, but it also gives us an insight into how PowerShell views the registry as a drive.

Topics for PowerShell’s Get-PSDrive Cmdlet

 ♣

List PowerShell’s PSProviders

This is how to enumerate PowerShell’s ‘Providers’.  It is interesting to see how Microsoft designed PowerShell to view the registry, functions and variables as ‘drives’.

# Get-PSDrive
Get-PSDrive

List Disk Capacity and Calculate FreeSpace %

  1. Let us begin by filtering for just disk drives using the parameter -FileSystem
  2. A check with Get-Member reveals there is no drive capacity property, however, we can add .free and .used to create this property.
  3. Once we have the total capacity for each drive, we can do the math to calculate the % freespace.
  4. The rest of the script is concerned with creating a pretty display using PowerShell’s -f format command.

Clear-Host
$Drives = Get-PSDrive -PSProvider FileSystem
ForEach ($Drive in $Drives) {
"{0,2} {1,8:N0} {2,6:P0}" -f $Drive.Name,
($Drive.Free/1gb + $Drive.Used/1gb),
($Drive.free / (($Drive.free +0.001) + $Drive.used))
}

Note 1:  I added a tiny amount to $Drive.free to swerve the divide by zero error for blank drives.

Note 2: You could create labels at line 3 by adding:
 "{0,2} {1,4:N0} {2,10:P0}" -f "Drive", "GB", "FreeSpace"

An Alternative Method Using Format Table

This script also lists the disk drives with their capacity in GB (gigabytes) and % freespace, but it uses a slightly different approach.

Clear-Host
Get-PSDrive -PSProvider FileSystem | Format-Table Name,
@{Name="Disk Size(GB)";Expression={"{0,8:N0}" -f($_.free/1gb +$_.used/1gb)}},
@{Name="Free (%)";Expression={"{0,6:P0}" -f($_.free / ($_.free +$_.used))}} `
-AutoSize

Note 3: Measure-Command reveals little difference in the speed of my two Get-PSDrive techniques.  However for this task, Get-WmiObject is much faster. See WMI disk method.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Get-PSDrive’s Properties

Let us used the trusted PowerShell research cmdlet with an alias of GM (Get-Member).

# Investigate Get-PSDrive’s Properties and Methods
Clear-Host
Get-PSDrive | GM # Get-Member

Note 4: This is how I discovered a useful ScriptProperty called .free and another called .used.

Real-life Task: Delete Old Files in Recycle Bin

Here is a script which could delete files in your recycle bin that are older than 100 days. 

Key point: you need to understand the difference between Get-Item and Remove-Item -recurse before it will deliver it’s potential, and actually zap files in the recycle bins in the various drives.

# PowerShell script to list files in the Recycle bin, which are older than 100 days
Clear-Host
ForEach ($DriveLetter in Get-PSDrive -PSProvider FileSystem) {
$DelPath = $DriveLetter.Name + ‘:\$Recycle.Bin’
Get-ChildItem $DelPath -Force -Recurse -ErrorAction SilentlyContinue |
Where-Object { (Get-Date).AddDays(-100) -gt $_.LastWriteTime } |
Get-Item
}

Note 5: Replace Get-Item with ‘Remove-Item -recurse, but only make the change if you understand the implications.

Note 6: You may wish to adjust the value for .AddDays(-100).

Note 7: Once you understand that this script can clean-up the recycle bin, you may consider adding to a Scheduled Task.

Solarwinds Config GeneratorGuy Recommends: The Free Config Generator

SolarWinds’ Config Generator is a free tool, which puts you in charge of controlling changes to network routers and other SNMP devices.  Boost your network performance by activating network device features you’ve already paid for.

Guy says that for newbies the biggest benefit of this free tool is that it will provide the impetus for you to learn more about configuring the SNMP service with its ‘Traps’ and ‘Communities’. Try Config Generator now – it’s free!

Download your free copy of Config Generator

Get-PSDrive Alternative Providers

Most of my examples focus on the FileSystem, however you can employ Get-PSDrive to interrogate the Registry, the Environment, PowerShell’s Aliases or Variables, the secret is to specify with -PSProvider.

Clear-Host
Get-PSDrive -PSProvider Registry | Ft -AutoSize

Note: You could substitute Alias, or Variable for ‘Registry’.

The Truth! Forget Get-PSDrive, Get-ChildItem is all you need.  Just remember that colon at the end of the PSProvider, thus:

Clear-Host
Get-ChildItem Alias:
# Get-ChildItem Variable:

Research Similar PowerShell Cmdlets

# PowerShell PSDrive Nouns and Similar
Clear-Host
Get-Command | Where {$_.Noun -Match ‘PSD’ -or $_.Noun -Match ‘PSP’}

See more on PowerShell PSProvider ยป

Summary of PowerShell Get-PSDrive

This cmdlet has a useful parameter called -PSProvider, it enables us not only to research the local disk structure, but also gives us an insight into how PowerShell accesses the registry as a drive.

If you like this page then please share it with your friends

 


See more Windows PowerShell  examples of variables

Syntax   • PowerShell Variables   • Get-PSProvider   • PowerShell Dollar Variable

PowerShell Functions   • Get-PSDrive   • PowerShell New-PSDrive   • Remove-PSDrive

PowerShell Home   • Foreach loops   • PowerShell Foreach   • Foreach-Object cmdlet

Please email me if you have a better example script.  Also please report any factual mistakes, grammatical errors or broken links, I will be happy to  correct the fault.