How To Use New-PSdrive Cmdlet In Powershell? 7 Best Examples

The New-PSDrive cmdlet allows us to map disks to local folders and registry entries on our PC in addition to network drives. Drives produced by the cmdlet can be either persistent so they can be used in Explorer or temporary so they are only accessible by the current PowerShell sessions.

 Use New-PSdrive Cmdlet In Powershell

I’ll go through all of the New-PSDrive cmdlet’s options in this article along with some practical examples you may utilize.

Use New-PSdrive Cmdlet In PowerShell

1. Using Get-PSDrive to inspect PS Drives

Several default drives are included when PowerShell is installed. Use the Get-PSDrive command to view these drives. PowerShell will display a variety of drives, including well-known ones like registry drives like HKLM or HKCU and file system drives like C.

Get-PSDrive
Get-PSDrive

The type of data that PS drives contain varies depending on the supplier. PowerShell makes use of these providers to make the data stored on each drive accessible. As you can see in the screenshot above, eight distinct providers of drives were returned by Get-PSDrive.

Run Get-PSProvider to find all installed providers on your system. The capabilities of various providers, determine how each service interacts with the active PowerShell session.

A few useful arguments, like the PSProvider parameter, are also available for the Get-PSDrive. You can use this argument to filter the PS drives that a specific provider returns. Get-PSDrive only displays drives that are accessible through the FileSystem provider and credential.

Get-PSDrive -PSProvider FileSystem
Get-PSDrive -PSProvider FileSystem

2. How to Use PS Drives In Powershell

Once you are aware of the PS drives that are accessible, you can investigate what is included in each one. The environment variables for Windows are stored on the Env drive, which is a standard PS drive. You can browse environment variables just like you would with a file system.

As with a file system, use cd or Set-Location to switch to the drive letter, then execute ls or Get-ChildItem, as shown below, to traverse the Env drive.

A colon is required to denote a PS drive. Once ls has been executed, you will see that PowerShell provides key/value pairs for each Windows environment variable.

cd Env:
ls
cd Env:
ls

3. Using New PSDrive to Create New PS Drives

You now know that PowerShell has a few pre-installed PS drives, but you can also build your own with the New-PSDrive cmdlet.

Creating “shortcuts” to file system directories or even mapping network drives using the FileSystem provider is one of the most popular uses of the New-PSDrive cmdlet. You can give labels to different file system locations using New-PSDrive.

4.  File Share Mapped Using New-PSDrive

For instance, you might need to map an SMB file share to a drive letter if it exists on your network somewhere. You may create a script, use PowerShell if you prefer the command line, or utilize the GUI to map that network disk.

Consider that your network share is accessible via the UNC route \\localhost\PSDRIVE.

Only as an example, the local computer (localhost) will be utilized. The same method can be used with New-PSDrive to map actual network disks.

For New-PSDrive to map a network drive, you must:

The name you’d like to give the drive (NetShare).
the FileSystem-based provider that the disk will utilize (PSProvider).

\\localhost\PSDRIVE utilizing the Root argument or the file share’s path.

Launch New-PSDrive and Get-ChildItem to explore the file share’s contents.

New-PSDrive -Name "NetShare" -PSProvider "FileSystem" -Root "\\localhost\PSDRIVE"
Get-ChildItem -Path NetShare:
New-PSDrive -Name "NetShare" -PSProvider "FileSystem" -Root "\\localhost\PSDRIVE"

You’ll observe that referencing NetShare no longer differs from referencing \\localhost\PSDRIVE directly.

A few notes must be added to your new drive. Perhaps the document storage file share? Use the Description argument with a value of “The file share to store documents” for example.

Using a PS Drive Filesystem for the Windows GUI

The PS drive only appears when PowerShell is running. Technically, using New PSDrive in this way does not map a network drive, unlike how the GUI does. However, it is still possible if the Persist parameter is used.

With the exception of PowerShell, the Persist options instruct New PSDrive to expose the disk to Windows. With the command, you could map an X drive to the WDMYCLOUDGarry file share.

New-PSDrive -Name "X" -PSProvider "FileSystem" -Root "\\WDMYCLOUD\Garry" -Persist
New-PSDrive -Name "X" -PSProvider "FileSystem" -Root "\\WDMYCLOUD\Garry" -Persist

5. Making Long Directory Paths Work with Filesystem PS Drives

The New-PSDrive cmdlet allows you to construct PS drives in addition to mapping file shares, which will save you some typing time. You can avoid switching to a directory called by making a drive based on a lengthy directory path. 

Microsoft SQL Server2019 saves its logs by default under the C:\Program Files\Microsoft SQL Server\MSSQL15.SQL2019\MSSQL\Log directory, which will be used to illustrate a real-world scenario. It would quickly grow tiresome to type that in each time you needed to change to that directory in PowerShell.

Create a PS drive called sqllog with New-PSDrive to avoid inputting a lengthy directory path. Once it has been constructed, change to the direct directory path instead using the PS drive name.

New-PSDrive -Name sqlLog -PSProvider "FileSystem" -Root "C:\Program Files\Microsoft SQL Server\MSSQL15.SQL2019\MSSQL\\og"
Set-Location sqlLog:
New-PSDrive

6. Utilizing the Registry To Run New-Psdrive

PS drives can be used for more than simply the file system. Recall that PS drives utilize a variety of suppliers. The register is one of those service providers. You can make “shortcuts” to any registry key you choose using the method you’ve learned.

Pass the Registry argument to the PSProvider parameter and, instead of using a directory path for the Root, use a registry key path, as shown below, to establish PS drives on a registry provider. Notably, this example also placed a description on the PS drive using the optional Description parameter.

New-PSDrive -Name "myRegistry" -PSProvider "Registry" -Root "HKLM\Software" -Description 'my local Registry key'

Run Get-PSDrive after creation to review your work. The Format-Table cmdlet is used in the example below to restrict output to just the Name and Description properties. By default, the Description attribute is hidden.

7. Using Remove-PSDrive to delete PS Drives

Make use of Remove-PSDrive. Using the PowerShell pipeline is one of the easiest ways to use the Remove-PSDrive cmdlet. You can remove one or more drives simultaneously by “piping” PS drives to Remove-PSDrive.

Instead of executing Remove-PSDrive three times, use Get-PSDrive to read each disk and then pipe it to Remove-PSDrive, as demonstrated below.

Get-PSDrive -Name NetShare, sqlLog, X | Remove-PSDrive -Force -Scope Global

Take note that each PS drive was previously established, but that performing the aforementioned command has eliminated them all.

The Force and Scope parameters are optional, so take note of that. When using the New-PSDrive -Scope argument, drives defined in the global scope are removed while using the Force parameter, a PS drive is removed even if a file is open.

Meet the Author

Abdul Rahim has been working in Information Technology for over two decades. Learn how Abdul got his start as a Tech Blogger , and why he decided to start this Software blog. If you want to send Abdul a quick message, then visit his contact page here.