Introduction to Drives and Providers in PowerShell

PowerShell includes the concept of "drives" which are data stores that can be accessed like traditional file system drives. Drives in PowerShell can map to the filesystem, registry, certificate stores, and more. The components that facilitate accessing these various data stores are called "providers".

Some common providers in PowerShell include:

  • FileSystem – for accessing files and folders on disk
  • Registry – for accessing the Windows registry
  • Certificate – for accessing x509 certficiates
  • Variable – for accessing PowerShell variables

The Get-PSDrive cmdlet allows us to see all the active drives mounted in a PowerShell session along with details on what providers are powering access to the underlying data stores.

Get-PSDrive Cmdlet Syntax and Parameters

The Get-PSDrive cmdlet has the following syntax:

Get-PSDrive [[-Name] <String[]>] [-Scope <String>] [-PSProvider <String[]>] [<CommonParameters>]

The key parameters are:

  • -Name – Specifies drive names to get details on. Omit to get all drives.
  • -Scope – Filter by a particular scope like Global, Local, Script, Private. Defaults to all scopes.
  • -PSProvider – Filter drives by one or more provider names.

For example, to get all drives in the current session:

Get-PSDrive

To get the C: drive only:

Get-PSDrive C

To get drives from the FileSystem provider:

Get-PSDrive -PSProvider FileSystem

Scopes for Filtering Drives

Using the -Scope parameter allows narrowing drives by their scope:

Scope Description
Global Available in all scopes and sessions
Local Only visible in current scope
Script Defined in current script or module
Private Only accessible privately to current scope

For example, to show only globally available drives:

Get-PSDrive -Scope Global

And to list script-specific drives:

Get-PSDrive -Scope Script 

Use Cases for Get-PSDrive

Some common scenarios where Get-PSDrive is helpful include:

Enumerating Logical Drives

Use Get-PSDrive to easily list all logical drives on a system by looking at the roots returned:

Get-PSDrive | Where-Object {$_.Provider -like "FileSystem"} | Select-Object Name, Root 

Name Root                         
---- ----                         
C    C:\                        
D    D:\

Listing Network Drives

Finding mapped network drives is also straightforward with Get-PSDrive:

Get-PSDrive -PSProvider FileSystem | Where-Object {$_.DisplayRoot -like "\\*"} | Select-Object Name, DisplayRoot

Name DisplayRoot               
---- ------------               
Z \\fileserver\share
X \\nas01\data

Auditing Accessible Drives

To perform security auditing of what drives are visible to users, Get-PSDrive provides an easy programmatic interface:

Get-PSDrive | Select-Object Name, Root, CurrentLocation, Description, Free, @{Name="Accessible?"; Expression={Test-Path $_.Root}}

This surfaces accessibility as well as usage statistics.

Troubleshooting Drive Issues

If dealing with an inaccessible drive, Get-PSDrive can help diagnose connectivity problems:

Get-PSDrive X | Format-List * 

Name           : X
Provider       : FileSystem
Root           : \\server\share
CurrentLocation : 
DisplayRoot    : \\server\share
Description    : 
Capabilities   : {}
FileSystemNames : {X} 
MachineName    : . 
ServiceName    : 
Drives         : {X}
MaximumComponentLength : 255
FreeSpace      : 5 GB
ErrorRecord    : Access denied

Inspecting the ErrorRecord property exposes lower-level errors explaining why the drive isn‘t accessible.

Discovering Drives from Other Providers

In addition to file system drives, Get-PSDrive allows listing drives mapped by other providers:

To show registry drives:

Get-PSDrive -PSProvider Registry 

For certificate stores:

Get-PSDrive -PSProvider Certificate 

And even drives that expose PowerShell variables:

Get-PSDrive -PSProvider Variable

These expose entirely different namespaces beyond the file system.

Key Properties for Understanding Drives

Some notable properties exposed by Get-PSDrive include:

CurrentLocation – The current working folder on the drive similar to Get-Location

DisplayRoot – A friendly view of the Root path

Description – Optional details that explain the drive‘s purpose

Free – Free space available on the drive (may not apply to non-storage providers)

These give rich metadata for discovering, describing and diagnosing drives.

Formatting and Filtering Output

To customize Get-PSDrive‘s output, pipe to formatting cmdlets:

Get-PSDrive | Format-Table -AutoSize 

Get-PSDrive | Format-List *

We can also filter on properties using Where-Object:

Get-PSDrive | Where-Object {$_.Free -gt 10gb}

Get-PSDrive | Where {$_.Name -like "H*"} 

Or select output columns with Select-Object:

Get-PSDrive | Select-Object Name, Root, Used, Free, Provider

Allowing full control over drive data extracted.

Connecting Drives to PowerShell Applications

Beyond interacting interactively, Get-PSDrive also helps identify drives mounted behind the scenes when loading modules and snap-ins.

For example, when using the SQL Server Provider:

Import-Module SQLPS   

Get-PSDrive -PSProvider SqlRegistry

Name           Provider      Root                                   CurrentLocation
----           --------      ----                                   ---------------
HKLM           Registry      HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft SQLSERVER

Similarly, the Exchange and SharePoint PowerShell modules expose their own drives. Get-PSDrive allows inspecting their presence.

Impersonation Drives

When utilizing impersonation or acting on behalf of other users, specialized drives also get mounted:

$cred = Get-Credential
Enter-PSSession -ComputerName sql1 -Credential $cred

Get-PSDrive | Where-Object {$_.Name -like "SQL*"}  

Name           Provider      Root                                   CurrentLocation  
----           --------      ----                                   ---------------
SQL             SqlRegistry   HKEY_LOCAL_MACHINE\SOFTWARE\Micr... SQLSERVER...
SQLPolicy       SqlRegistry   HKEY_LOCAL_MACHINE\SOFTWARE\Micr... SQLServer

Notice these include the accessing user identity information in their path.

Security Considerations

When enumerating available drives, be mindful of permissions.

Use the most restrictive scoping possible depending on whether only inquiring locally or across a network. Avoid probing networked drives unnecessarily without cause.

Also understand some drives don‘t mount until actually accessed. So providers may load dynamically when inspecting certain PSDrives.

Measure twice, probe once. And don‘t touch drives without reason. Their contents or metadata could be sensitive.

Comparing Get-PSDrive to Related Commands

While Get-PSDrive focuses specifically on drives, some related PowerShell discovery commands include:

Get-PSProvider – Lists registered PowerShell providers

Get-Module – Shows PowerShell modules loaded

Get-PSSession – Gets active PSSessions for remote commands

Get-PSDrive combined with these others can establish a whole picture of accessible data stores across local and remote sessions in PowerShell.

Conclusion

The Get-PSDrive cmdlet enables both systems administrators and developers to better understand mapped drives and providers. It reveals access and origins spanning files systems, databases, certificates, registries and more.

Mastering Get-PSDrive unlocks discovering what PowerShell namespaces are available to interact with during operations or coding. It should be a standard part of any PowerShell toolbox to examine session environments.

With robust filtering, formatting and comparisons to related PowerShell metadata commands, the insightful data and control Get-PSDrive delivers is invaluable.

Similar Posts