Viewing all accessible drives in PowerShell

I noticed that cd \ navigates to the root of my drive.

But there are other drives that I can access on my computer.

Is there a command in PowerShell that lists all of the drives that I can connect to?

2 Answers

This somewhat depends on what you're calling a "drive". In PowerShell, there are drives that represent your usual local or network-mapped volumes, as well as drives which hold other objects like Aliases, Environment Variables, Functions, PowerShell Variables, Registry Hives, and more.

To see a list of all "drives" in PowerShell, use:

Get-PSDrive

Or you can use the built-in alias:

gdr

(Anywhere you see Get-PSDrive below, you may substitute gdr instead.)

To get only FileSystem drives, e.g.: local or network-mapped volumes or media bays, use:

Get-PSDrive -PSProvider FileSystem

To get just local drives, or just hard drives, I couldn't find an appropriate Property or Method in the objects output by Get-PSDrive to filter by. A little Googling later, I found Microsoft's Get-PSDrive documentation on TechNet. There, along with details of how to use Get-PSDrive, a few other PowerShell-accessible methods of drive enumeration are also listed. One I found useful is via the System.IO.DriveInfo class in the .NET Framework.

The below command, executable via PowerShell, will list only local hard drives.

[System.IO.DriveInfo]::getdrives() | Where-Object {$_.DriveType -eq 'Fixed'}

If you want to include any local drive - not just hard drives - use this:

[System.IO.DriveInfo]::getdrives() | Where-Object {$_.DriveType -ne 'Network'}

If you want to only see drive letters, pipe the output to Select-Object, like this:

[System.IO.DriveInfo]::getdrives() | Where-Object {$_.DriveType -ne 'Network'} | Select-Object -Property Name

Note that Where-Object and Select-Object also have built-in aliases of ? and select, respectively. (Where-Objectis also usable aswhere` - it's just really a matter of preference.) The System.IO.DriveInfo class can also be shortened to just IO.DriveInfo. So, that last command could be run as this, if you like:

[IO.DriveInfo]::getdrives() | ? {$_.DriveType -ne 'Network} | select -Property Name

Of course, there's plenty else you can do with Get-PSDrives, and the .NET classes accessible via PowerShell, as well as many other PowerShell shortcuts available. I suggest reading up more on TechNet and similar sites, and using the Get-Help and Get-Command cmdlets to gain better familiarity with the environment.

Use following command in powershell to list all drives on your PC

gdr -PSProvider 'FileSystem'

or

Get-PSDrive -PSProvider 'FileSystem'

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like