Get-PSDrive PowerShell Command

Get-PSDrive | Taking on PowerShell one cmdlet at a time | Weekly Blog

Share this post:

This is a part of an on-going blog series written by Adam Gordon. Each week, Adam will walk you through a PowerShell command, showing you when and how to use each one. This week, Adam covers Get-PSDrive.

When to use Get-PSDrive?

The Get-PSDrive cmdlet gets the drives in the current session. You can get a particular drive or all drives in the session.

This cmdlet gets the following types of drives:

  • Windows logical drives on the computer, including drives mapped to network shares.
  • Drives exposed by PowerShell providers (such as the Certificate:, Function:, and Alias: drives) and the HKLM: and HKCU: drives that are exposed by the Windows PowerShell Registry provider.
  • Session-specified temporary drives and persistent mapped network drives that you create by using the New-PSDrive cmdlet.

Beginning in Windows PowerShell 3.0, the Persist parameter of the New-PSDrive cmdlet can create mapped network drives that are saved on the local computer and are available in other sessions.

Also, beginning in Windows PowerShell 3.0, when an external drive is connected to the computer, Windows PowerShell automatically adds a PSDrive to the file system that represents the new drive. You do not need to restart Windows PowerShell. Similarly, when an external drive is disconnected from the computer, Windows PowerShell automatically deletes the PSDrive that represents the removed drive.

What version of PowerShell am I using?

Get the PowerShell Version from your machine:

$PSVersionTable

This command shows you the PowerShell version information on your machine.

How to use Get-PSDrive?

Get drives in the current session:

Get-PSDrive

This command gets the drives in the current session.

The output shows the hard drive (C:), CD-ROM drive (D:), and the drives exposed by the Windows PowerShell providers (Alias:, Cert:, Env:, Function:, HKCU:, HKLM:, and Variable:).

Get a drive on the computer:

This command gets the C: drive on the computer. Note that the drive letter in the command is not followed by a colon.

Get-PSDrive C

Get all the drives that are supported by the Windows PowerShell file system provider:

This command gets all of the drives that are supported by the Windows PowerShell FileSystem provider. This includes fixed drives, logical partitions, mapped network drives, and temporary drives that you create by using the New-PSDrive cmdlet.

Get-PSDrive -PSProvider FileSystem

Check to see if a drive is in use as a Windows PowerShell drive name:

This command checks to see whether the X drive is already in use as a Windows PowerShell drive name. If it is not, the command uses the New-PSDrive cmdlet to create a temporary drive that is mapped to the HKLM:\SOFTWARE registry key.

if (Get-PSDrive X -ErrorAction SilentlyContinue) {
    Write-Host ‘The X: drive is already in use.’
} else {
     New-PSDrive -Name X -PSProvider Registry -Root HKLM:\SOFTWARE
}

Learn last week’s command: Get-Location.

Need PowerShell training? Check out ITProTV’s PowerShell online IT training courses.