How To Map A Drive In PowerShell

This Itechguide teaches you how to map a drive in PowerShell. The guide covers 5 methods to map a drive in PowerShell.

In the first method, you will learn how to map a network share. Then, you will learn how to map a local folder as a drive.

In the last two methods, I will teach you how to map a network drive with PowerShell hashtable – and how to map a registry key as a drive.

Drives mapped with the New-PSDrive PowerShell Cmdlet (methods 1 to 4) do not appear on “This PC” (File Explorer). To map a network drive that will appear in “This PC”, use method 5.

How To Map A Network Share In PowerShell

How To Map A Network Share In PowerShell

The most common task for SysAdmins is to map network drives. There are multiple methods to map network drives.

However, like most Windows tasks, you can map a network share with PowerShell.

To map a network share, run a command similar to the one below:

New-PSDrive -Name "N" -PSProvider "FileSystem" -Root "\\DESKTOP-SIAQMO1\Log Files"

The Name is like the drive letter. Change this to whatever you want. The name can also be a word.

You do not need to change the PSProvider FileSystem is the PSProvider for folders. However, you have to change the Root – path to your network share.

Once you have modified the command to meet your needs, to map the network drive, enter the command in PowerShell and press the enter key on your keyboard. The drive will be mapped in the current PowerShell session.

It will also display the details of the mapped drive.

In the examples above, the network drives mapped by PowerShell are NOT persistent. The implication is that if I close my current PowerShell session, the mapping will be lost.

To map a persistent network share with PowerShell, add the Persist parameter. Here is the modified command that will map a shared network drive as a persistent drive:

New-PSDrive -Name "N" -PSProvider "FileSystem" -Root "\\DESKTOP-SIAQMO1\Log Files" -Persist

Moreover, the New-PSDrive command has another parameter, Credential that allows you to specify a user name and password to access a network share you want to map.

You will need to add the Credential parameter if the account you are currently logged in with does not have access to the drive you want to map. To specify a credential, use the DomainName\UserName format.

Finally, here is the modified New-PSDrive command that will map a persistent network share with a specified credential:

New-PSDrive -Name "N" -PSProvider "FileSystem" -Root "\\DESKTOP-SIAQMO1\Log Files" -Persist Credential DomainName\UserName 

If you press enter key to execute the command, PowerShell will request the password for the UserName.

How to Map A Local Folder As A Drive In PowerShell

How to Map A Local Drive In PowerShell

When you map a network drive, you connect a shared folder on a remote computer to your local computer. However, you can also map a folder on your PC as a network drive.

The process for mapping a network drive is the same as mapping a local folder as a drive. To map a local folder as a drive, modify the Root parameter of the New-PSDrive command.

Instead of specifying the path to a network share in the Root parameter – you specify a path to a local folder. In the example, I want to map my Document folder to a drive called Document.

Here is the modified command:

New-PSDrive -Name "Document" -PSProvider "FileSystem" -Root "C:\Users\victo\Documents"

Here is the result of the command.

How To Map A Drive Using PowerShell Hashtable

How To Map A Drive Using PowerShell Hashtable

In the first two examples above, we mapped network drives with PowerShell by entering the parameters of the New-PSDrive command directly in the command.

However, in some scripting situations, you may decide to create a hashtable that defines the parameters. Then, use the hashtable in the New-PSDrive command.

This method is a 2-step process. The first step is to create the hashtable. Then, in the last step you run the New-PSDrive command.

Before I proceed with the example in this section, refer to the last command.

New-PSDrive -Name "Document" -PSProvider "FileSystem" -Root "C:\Users\victo\Documents"

In this example, I will add the Name, PSProvider, and Root parameters in a hashtable, $documents. Here is a sample hashtable with these details:

$parameters = @{
     Name = "Document"
     PSProvider = "FileSystem"
     Root = "C:\Users\victo\Documents"
 }

Copy it to a new PowerShell ISE document (modify the values as required). Then, beneath the hashtable, add the New-PSDrive command as shown below:

New-PSDrive $parameters

The full command should be as shown below:

$parameters = @{
     Name = "Document"
     PSProvider = "FileSystem"
     Root = "C:\Users\victo\Documents"
 }
 New-PSDrive @parameters

Finally, to execute the command, click the Run Script icon (highlighted in the screenshot below). The script will run and display the results of the mapped drive at the bottom of PowerShell ISE.

How To Map A Drive Using PowerShell Hashtable

How To Map A Drive For A Registry Key

How To Map A Drive For A Registry Key

Before you read this section, I strongly recommend that you read our article on How to Use PowerShell to Read Registry Value.

So far, we have covered steps to map folders as drives with PowerShell. However, you can also map a registry key as a drive.

The command to map a registry key as a drive with PowerShell is similar to the command used to map folders. The only difference is that instead of specifying PSProvider as “FileSystem”, you specify it as “Registry”.

Additionally, instead of specifying the Root as a folder or share path, you will specify a path to a Windows registry key.

Based on the above information, to map the registry key HKEY_CURRENT_USER\Control Panel, I will use the command below:

New-PSDrive -Name "CPRegistry" -PSProvider "Registry" -Root "HKCU:\Control Panel"

When you run the command, PowerShell will map the registry path with the specified parameters – then, display the details of the mapped drive.

How To Map A Network Drive With The MapNetworkDrive Function

How To Map A Network Drive With New-Object Cmdlet

All the examples I have given so far use the New-PSDrive Cmdlet to map a drive in PowerShell. There are two limitations to the New-PSDrive Cmdlet methods:

  1. The New-PSDrive Cmdlet is available from PowerShell version 3.0. So, in the rare instance that you still use PowerShell version 2.0, you will not be able to use any of the methods discussed above
  2. When you map a drive with New-PSDrive Cmdlet, the mapped drive is only available in PowerShell. The drive will not be available in File Explorer

If you want to overcome these two limitations, use the method discussed in this section – to map a drive in PowerShell. To make it easy to understand, this section is divided into two sub-sections.

Note that the method discussed in this section can only be used to map a network share. Unlike mapping local drives or registry keys with New-PSDrive command, the Wscript.Network function can only map a shared folder in a remote computer.

Create A Wscript.Network ComObject

The first step to use this method to map a drive in PowerShell is to create a Wscript.Network ComObject – then save it in a variable. Here is the command that does the job:

$WscriptNetwork = New-Object -ComObject "Wscript.Network"

Before you proceed, open a PowerShell prompt and run the command. The ComObject will be created.

In the next section, we map network drive

Map Network Drive With The MapNetworkDrive Function

Now that you have created a Wscript.Network ComObject, you can use its MapNetworkDrive function to map a network drive. Here are the commands:

$WscriptNetwork.MapNetworkDrive("K:", "\\DESKTOP-SIAQMO1\Log Files")
$WscriptNetwork.MapNetworkDrive("K:", "\\DESKTOP-SIAQMO1\Log Files", $True)

The first command maps the network share “Log Files” on DESKTOP-SIAQMO1 to drive K. However, the mapping is not persistent.

On the contrary, the second command maps the same share but makes it persistent. What makes the mapping persistent is the $True parameter of the MapNetworkDrive function.

Moving on, I will run the last command. Like the first command, the first command does not display any results.

However, there are two methods you can confirm that the drive is mapped.

Method 1: Run the Set-Location command below to point to the drive letter – in my example, K.

Set-Location P:\

Finally, if you want to list the content of the folder, run the Get-Childitem command.

Get-Childitem

I have displayed the output of the Get-Childitem command and the content of the folder in File Explorer in the screenshot below.

Method 2: Another method to confirm that the network share was mapped is to open “This PC” in File Explorer.

There you have it – 5 methods to map a drive in PowerShell!

I hope you found the methods to map drive with Windows PowerShell discussed in this guide helpful.

If you found it helpful, kindly vote Yes to the “Was this post Helpful” question below.

However, if none of the methods worked for you or you needed something other than the discussed methods, click No to the “Was this post Helpful” question below. Then, kindly provide us additional details. We review our reader’s feedback and update our guides to constantly meet your needs.

Alternatively, you could ask a question, leave a comment or provide feedback with the “Leave a Reply” form found towards the end of this page.

Finally, for more PowerShell tech Itechguides, visit our Windows PowerShell How-To guide page. You may also find our Work from Home page very helpful.

Victor Ashiedu

Victor Ashiedu is the Managing Editor of Itechguides.com. Before he started Itechguides.com, he worked in IT for close to 20 years. Having done all things tech all his career, he now focuses on making tech easier for others. Victor writes "How to" guides, "Fix it" guides, reviews and buying guides.

2 Comments
Show all Most Helpful Highest Rating Lowest Rating Add your review
  1. “When you map a drive with New-PSDrive Cmdlet, the mapped drive is only available in PowerShell. The drive will not be available in File Explorer”

    This is not true. You can make the drive available to File Explorer simply by adding -Scope Global. If you’re mapping from a script, you also need to dot-source the script.

    • Hello Mike,

      Thank you for your comment and feedback. Unfortunately, I ran New-PSDrive with -Scope Global but the mapped drive did not appear on File Explorer.

      I’ll appreciate it if you can share the exact command and maybe screenshots for our readers.

      Thanks again.

Leave a reply

Itechguides.com
Logo
Enable registration in settings - general
Compare items
  • Laptops (0)
  • PCs & Macs (0)
  • Smartphones (0)
  • Processors (0)
  • Televisions (0)
  • Tablets (0)
Compare
0
Shopping cart