Updated March 6, 2023
Introduction to PowerShell Map Network Drive
Often developers or users come across scenarios where they are required to access files that are kept in a centralized location. These centralized locations are called as network drives. It is not feasible to log in to that server to access those files every time; in such scenarios, these network drives are mapped on to the user’s local system. This article will explain in detail the various ways of mapping a network drive on to a local computer. In this topic, we are going to learn about PowerShell Map Network Drive.
Different ways of mapping a network drive
The simple and foremost way of mapping a network drive is to use a command prompt and run the following command
net use Driveletter: Path to be mapped
Net use k:\\ Vignesh\test
In the above example, the network shared location test will be mapped to the K drive.
If the network drive requires authentication to access, it will be tedious to provide the credentials every time. In order to avoid the credentials can be supplied as part of the command
net use K: \\vignesh\test /user:vignesh Pass@123
The main to keep in mind while using the above command is that the mapping will be lost once the system is rebooted. In order to overcome that and make a permanent mapping, the persistent keyword should be enabled.
net use K: \\vignesh\test /user:vignesh Pass@123 persistent:Yes
Once the persistent is enabled, the created drives will be mapped permanently unless it is set to No explicitly.
The above method is an old one, and it is not supported in PowerShell anymore.
The currently used method is by using the New-PSDrive cmdlet.
Create network drive using New-PSDrive
The New-PSDrive cmdlet is used for creating both temporary and permanent network mapped drives. Temporary drives are available only in the current session of PowerShell. It can have any name and can be mapped to any remote computer, a directory or a data store. Temporary mapped drives are created whenever there is a need to access date and perform some operation. Since the temporary drives are associated with PowerShell, they can not be accessed from file explorer, command prompt, WMI or .net framework. This cmdlet was introduced in PowerShell 3.0. The persist parameter of this cmdlet is used to create mapped drives that are associated with windows. These drives are not based on sessions.
They are saved and can be accessed easily like other drives using file explorer. When the New-PSDrive cmdlet is used within a script and if the drive should be there indefinitely, then the script should be dot sourced. The best way to create a permanent mapping is to use the scope parameter and set its value to global. In case of an external drive is connected to a system, a PSDrive is automatically added to the system by PowerShell. Once the drive is disconnected, the mapping is also removed. A restart is not required in both cases. While creating if a UNC path is specified in the root parameter, then the credential specified is used to create the PSDrive.
Below are the different parameters:
-Confirm: This parameter is used to get confirmation from the user before running the cmdlet. The data type of this parameter is a switch. Its alias is cf. The default value is false. This parameter doesn’t accept both pipeline input as well as wildcard characters.
-Credential: This represents the user account under which the cmdlet must be run. By default, the current user’s credential is used. Username is stored in PSCredential object, and password is stored in the secure string format. The data type of this parameter is PSCredential. This parameter accepts pipeline input, but wildcard characters are not permitted.
-Description: This denotes the description of the drive that is being created. The data type of this parameter is a string. This parameter accepts pipeline input, but wildcard characters are not permitted. To find the description of all the drives, Get-PSDrive | Description cmdlet can be used. To get a driver’s description, Get-PSDrive Drive name. Description can be used.
-Name: This is the name of the drive that is being created. For permanent drive names, a letter can be used. For temporary PSDrives, any string can be used as the name. The data type of this parameter is a string. This parameter accepts pipeline input, but wildcard characters are not permitted. This is a mandatory parameter.
-Persist: This is used to create a window-associated network drive. These types of drives are not session-specific, and they can be viewed and accessed through file explorer. To create permanent drives, it is needed to set the scope parameter as global. When the scope is set to local, PSDrive is created within the scope in which cmdlet is run without dot sourcing. When the drive should persist indefinitely, a dot source should be used within the script. The name of the drive can be any letter, whereas the root parameter should be a UNC. When a windows mapped drive is disconnected, the mapping is permanently deleted. These mapped drives are user-specific. The data type of this parameter is Switch. The default value is false. It accepts pipeline input, whereas wildcard characters are not permitted.
-PSProvider: This denotes the provider that should be used for creating the drive. If the drive is associated to a file system, then the provider is a file system; in the case of a registry key, the provider is a registry. The data type of this parameter is a string. The default value is none. It accepts pipeline input, whereas wildcard characters are not permitted.
This denotes the location of the data store. Temporary PS drives can be associated with any of the local or remote computer or drive providers. Mapped drives can only be associated with a file system of a remote computer. The data type of this parameter is a string. The default value is none. It accepts pipeline input, whereas wildcard characters are not permitted.
-Scope: This is used to denote the scope of the drive that is created. It can be global, local, script, or a number related to scope. The current scope is denoted by 0, and the parent is represented by 1. The data type of this parameter is a string. The default value is local. It accepts pipeline input, whereas wildcard characters are not permitted.
-Whatif: This is kind of a sample output if the cmdlet is run. Its data type is the switch. Its alias is wi. The default value is false. It doesn’t accept both pipeline input, and wildcard characters are also not allowed.
Below is the example of PowerShell Map Network Drive:
Write-Host "Demo of mapping drive using powershell"
New-PSDrive -Name "test" -PSProvider "FileSystem" -Root "\\appserver\Public"
Write-Host "Drive created"
Write-Host "create a temporary drive"
New-PSDrive -Name "Test" -PSProvider Environment -Root "C:\Users\vignesh\Documents" -Description "Test"
Write-Host "Temporary drive created"
Write-Host "Drive for a registry key"
New-PSDrive -Name "test" -PSProvider "Registry" -Root "HKLM:\Software\test"
Write-Host "Registry drive created"
Demo of mapping drive using powershell
Conclusion – PowerShell Map Network Drive
Thus, the article various ways of creating network map drives using PowerShell. It also covered various parameters and their use and difference between temporary and permanent drives. To learn more in detail, it is advisable to write sample scripts and practice them.
This is a guide to PowerShell Map Network Drive. Here we discuss How to change the directory in PowerShell along with the examples and outputs. You may also have a look at the following articles to learn more –