Home » PowerShell » PowerShell – Map Network Drive

PowerShell – Map Network Drive

More often, PowerShell administrator needs to map network drive on local computer or remote computer so that shared file can be quickly accessible just like you would use your local drive. When you map network drive using PowerShell, it will create shortcut of shared drive or folder on network and map network drive to drive letter under This PC in Window Explorer.

Using PowerShell PS-Drive cmdlet, you can quickly creates temporary or persist network drives and map network drive to drive letter, directory on local computer or registry keys.

PowerShell map network drive command – New-PSDrive

Cmdlet: New-PSDrive

Description: Creates temporary or persists network drives for shared folder or location on local computer

Syntax:

New-PSDrive
   [-Name] <String>
   [-PSProvider] <String>
   [-Root] <String>
   [-Description <String>]
   [-Scope <String>]
   [-Persist]
   [-Credential <PSCredential>]
   [-WhatIf]
   [-Confirm]
   [<CommonParameters>]

There are different ways to map network drive in PowerShell, let’s understand mapping of network drive with examples.

PowerShell Map Network Drive Temporary Drive Letter

New-PSDrive in PowerShell map network drive temporary. Temporary drives are exists only in current PowerShell session and mapped it to any local computer or remote computer resource.

Using temporary drive in PowerShell session, you can access the data in associated data store.

To create and map network drive temporary in PowerShell, run below command

New-PSDrive -Name "K" -PSProvider "FileSystem" -Root "\\PS-101\FTP"

Name       Provider      Root
----       --------      ----
K          FileSystem    \\PS-101\FTP

In the above example, New-PSDrive command uses Name parameter to specify drive letter on local computer. PSProvider parameter specify PowerShell FileSystem provider and Root parameter specify the name of network shared path.

Above command on execute in PowerShell, map network drive temporary to drive letter K.

After you close the PowerShell session, it will disconnect temporary created drive, you can’t access drive by File Explorer or net use tool as mapped network drive not persistent.

To access the contents of newly map drive letter K in PowerShell session, use Get-ChildItem cmdlet with driver letter K

Get-ChildItem -Path K:

    Directory: \\PS-101\FTP


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       12-03-2021     10:20        3065560 anyconnect-win-3.1.05170-web-deploy-k9.exe
-a----       12-03-2021     10:16       49207400 CitrixReceiver.exe
-a----       12-03-2021     10:03      145547872 CitrixWorkspaceApp.exe


PS C:\>   

Cool Tip: Using Test Connection to ping list of computers in PowerShell!

How to map network drive to local folder

In the above example, we have mapped network drive for remote shared data store. Using New-PSDrive command, you can create temporary PowerShell drive that mapped to local folder on the local computer.

Lets consider an example to create temporary map network drive for C:\Users\ShellAdmin\Documents and mapped it as Document to quickly access documents

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

Name           Used (GB)     Free (GB) Provider      Root                                 CurrentLocation
----           ---------     --------- --------      ----                                 ---------------
Document            0.00         11.11 FileSystem    C:\Users\ShellAdmin\Docum...

In the above command, New-PSDrive uses Name parameter to specify Document drive.

PSProvider specify PowerShell FileSystem.

Root Specify path of local computer directory

Another method to create a temporary drive to local folder on local computer is

$parameters = @{
    Name = "Document"
    PSProvider = "FileSystem"
    Root = "C:\Users\ShellAdmin\Documents"
    Description = "Maps to ShellAdmin mydocument folder."
}
New-PSDrive @parameters

Name        Provider      Root
----        --------      ----
Document      FileSystem    C:\Users\ShellAdmin\Documents

After you close the PowerShell session, it will disconnect temporary created local directory drive, you can’t access drive by File Explorer or net use tool as mapped network drive not persistent.

Cool Tip: Use PowerShell script to get free disk space in PowerShell!

Create Temporary Drive for Registry Key

New-PSDrive command in PowerShell can be use to create a temporary drive for registry key. Using Get-ChildItem command, you can access the contents of Registry keys.

In the example, I have mapped HKLM:\Software\ShellGeek registry key to ShellGeek

New-PSDrive -Name "ShellGeek" -PSProvider "Registry" -Root "HKLM:\Software\ShellGeek"

In the above command, New-PSDrive uses Name parameter to specify ShellGeek drive.

PSProvider specify PowerShell Registry.

Root Specify path of registry location.

You can use Get-ChildItem cmdlet to access the contents of registry keys.

After you close the PowerShell session, it will disconnect temporary created drive to access registry keys, you can’t access drive by File Explorer or net use tool as mapped network drive not persistent.

Cool Tip: Do you know how to fix running PowerShell script disabled on System!

PowerShell Map Network Drive Persistent

Using PowerShell New-PSDrive Persist parameter, you can persist mapped network drives. This mapped drives are saved in Windows and are not session specific. Hence you can use File Explorer or net use tool to map network drive.

Let’s take an example to map network drive persistent in drive letter k, using below PowerShell script

New-PSDrive -Name "K" -PSProvider "FileSystem" -Root "\\PS-101\FTP" -Persist

Name           Used (GB)     Free (GB) Provider      Root                          CurrentLocation
----           ---------     --------- --------      ----                          ---------------
K                 252.37         30.57 FileSystem    \\PS-101\FTP


PS C:\>     

In the above command, New-PSDrive uses Name parameter to specify drive with Drive Letter K.

PSProvider specify PowerShell FileSystem.

Root Specify path of remote computer data store

It create persist network drive on your local system. You can access mapped network drive contents using Windows tools such as File Explorer or net use

Cool Tip: Using QUser to get list of users logged on to server in PowerShell!

PowerShell script to map network drive with credentials

To map network drive that authenticated with domain service account, use New-PSDrive Credential parameter.

Credential parameter use to get domain service account credentials for authentication.

Lets consider an below example, to map network drive which require authentication.

New-PSDrive -Name "K" -PSProvider "FileSystem" -Root "\\PS-101\FTP" -Persist -Credential ShellGeek\Admin

Name           Used (GB)     Free (GB) Provider      Root                          CurrentLocation
----           ---------     --------- --------      ----                          ---------------
K                 252.37         30.57 FileSystem    \\PS-101\FTP


PS C:\>

In the above command, New-PSDrive uses Name parameter to specify drive with Drive Letter K.

PSProvider specify PowerShell FileSystem.

Root Specify path of remote computer data store

Persist parameter specify to persist mapped network drive

Credential parameter specify domain service account credential and on command execution, it display dialog to enter username and password.

Upon successful authentication, it will map a network drive which is persistent.

You can access mapped network drive contents using Windows tools such as File Explorer or net use

Cool Tip: How to remove mapped network drive using PowerShell!

Conclusion

I hope above article on how to map network drive in PowerShell with different ways helpful to you.

You can create and map a temporary or persist drive based on using New-PSDrive parameter. Using Credential parameter, you can map network drive which needs authentication to access contents.

Use File explorer or net use tool to access map network drive content.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on ShellGeek home page.