How to List Drives in PowerShell

  1. Introduction to Get-PSDrive Command in PowerShell
  2. Get Drives in the Current Session in PowerShell
  3. Get a Drive on the Computer in PowerShell
  4. Get All the Drives that the PowerShell File System Provider Supports
  5. Check to See if a Drive is Used as a PowerShell Drive Name
How to List Drives in PowerShell

As a Windows administrator, there will be situations wherein we need to get specific information on a system that we are managing daily. One of this information is the system drive.

We can use PowerShell to query this information quickly. This article will discuss the benefits of the Get-PSDrive command and several situations where we can apply the said command in a real-world scenario.

Introduction to Get-PSDrive Command in PowerShell

The Get-PSDrive command retrieves the drive information in the current machine or session. We can either get a particular system drive or all drives in the computer.

This cmdlet retrieves information from the following types of drives.

  1. Windows logical drives on the computer are mapped to network shares, including drives.
  2. Drives exposed by PowerShell providers (such as Function:, Certificate:, and Alias: drives) and the HKLM: and HKCU: drives that the Windows PowerShell Registry provider exposes.
  3. Persistent mapped network drives or session-specified temporary drives that we create using the New-PSDrive cmdlet. Since Windows PowerShell version 3.0, the -Persist parameter of the New-PSDrive command can create mapped network drives saved on the local computer and available in other sessions.

We will tackle this in the next section of the article.

Since Windows PowerShell version 3.0, when an external drive (flash drives, external hard disks, etc.) is connected to the computer, Windows PowerShell automatically adds a PSDrive to the file system representing the new drive.

Therefore, you do not need to restart Windows PowerShell.

Similarly, Windows PowerShell automatically deletes the PSDrive that represents the removed drive when an external drive is disconnected from the computer.

To check your current PowerShell version, you may run the following snippet below.

$PSVersionTable

Here are some examples below that we can use when creating scripts with the Get-PSDrive command.

Get Drives in the Current Session in PowerShell

The command below gets all drives in the current session. We can also point out that the basic syntax of the cmdlet doesn’t require any additional arguments to execute.

Get-PSDrive

Get a Drive on the Computer in PowerShell

This command gets the specific drive on the computer. It is worth noting that a colon should not follow the drive letter in the command.

For example, we queried the system drive C: in the example code below. In addition, we may replace the letter C argument in the snippet to query another drive.

Example Code:

Get-PSDrive C

Output:

Name           Used (GB)     Free (GB) Provider      Root
----           ---------     --------- --------      ----
C                 471.64        459.13 FileSystem    C:\

Get All the Drives that the PowerShell File System Provider Supports

This command retrieves all the drives that the Windows PowerShell File System provider supports. This method includes fixed drives, logical partitions, mapped network drives, and temporary drives that you create using the New-PSDrive cmdlet.

Example Code:

Get-PSDrive -PSProvider FileSystem

Check to See if a Drive is Used as a PowerShell Drive Name

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

if (Get-PSDrive Q -ErrorAction SilentlyContinue) {
    Write-Host 'The Q: drive is already in use.'
}
else {
    New-PSDrive -Name Q -PSProvider Registry -Root HKLM:\SOFTWARE
}
Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn