Amazon holiday deals See deals →ad

PowerShell Script to Map Network Drive: Step-by-Step

Mapping a network drive in PowerShell lets you connect a shared folder to a drive letter from the command line, which is handy for scripts, remote sessions, and repeatable setups. This guide shows you how to write and run a small PowerShell script that maps a drive, makes it persistent, and handles credentials safely. You will need a Windows computer, access to a network share (its UNC path, such as \\server\share), and an account that has permission to use that share. Administrator rights are not always required, but some commands behave differently depending on your privileges and execution policy.

What You’ll Need

  • A Windows PC with PowerShell (built in on modern Windows versions).
  • The UNC path of the shared folder you want to map (for example, \\fileserver\team).
  • An account with read or write permission to that share.
  • An available drive letter that is not already in use.
  • Optionally, the credentials for that account if it differs from the one you are signed in with.

Step-by-Step

  1. Open PowerShell. Press the Start button, type PowerShell, and choose Windows PowerShell or PowerShell. For scripts that change system-wide settings, right-click and choose Run as administrator. Menu names vary slightly by Windows version.
  2. Check which drive letters are free. Run Get-PSDrive -PSProvider FileSystem to list current drives. Pick a letter that is not listed, such as Z.
  3. Decide on the command. The modern cmdlet is New-PSDrive. A basic mapping looks like this: New-PSDrive -Name Z -PSProvider FileSystem -Root \\server\share -Persist. The -Persist switch makes the drive survive a reboot and appear in File Explorer. Without it, the drive exists only for the current PowerShell session.
  4. Handle credentials if needed. If the share requires a different account, create a credential object first: $cred = Get-Credential. Enter the user name and password when prompted. Then pass it: New-PSDrive -Name Z -PSProvider FileSystem -Root \\server\share -Persist -Credential $cred. Avoid typing passwords directly into a script file.
  5. Save the script. Put your commands in a file with a .ps1 extension, for example Map-Drive.ps1. Keep the credential prompt or use a saved credential method rather than hard-coding a password.
  6. Set the execution policy if the script is blocked. Check it with Get-ExecutionPolicy. If scripts are blocked, you can run Set-ExecutionPolicy -Scope CurrentUser RemoteSigned after confirming with your IT policy. This changes a security setting, so only do it if you understand the implications or your administrator approves.
  7. Run the script. Navigate to the folder containing it and run .\Map-Drive.ps1. If you see an error about running scripts being disabled, revisit the execution policy step.
  8. Verify the mapping. Run Get-PSDrive -PSProvider FileSystem again, or open File Explorer and look for the new drive letter. You can also test access with Test-Path Z:\, which returns True if the drive is reachable.
  9. Remove the drive when no longer needed. Use Remove-PSDrive -Name Z. For a persistent mapping, you may also need to remove it from File Explorer or with net use Z: /delete if it was created that way.

Common Mistakes

  • Forgetting -Persist and wondering why the drive disappears after closing PowerShell.
  • Using a drive letter that is already taken, which causes an error or silently replaces an existing mapping.
  • Hard-coding passwords in the script. Use Get-Credential or a secure credential store instead.
  • Running the script under an account that lacks permission to the share, leading to access-denied errors.
  • Mixing up New-PSDrive with the older net use command. Both work, but they behave differently for persistence and credentials.
  • Ignoring execution policy errors and assuming the script itself is broken.

Tips

  • Use a drive letter well away from ones Windows or other tools commonly use, to avoid conflicts.
  • Add -Scope Global if you need the drive visible outside the current PowerShell session in some contexts.
  • Wrap the mapping in a short check: if Test-Path Z:\ is false, create the drive. This makes the script safe to run repeatedly.
  • Keep credentials out of version control. If you store scripts in a shared folder, use a credential manager or prompt at runtime.
  • Document the UNC path and drive letter in a comment at the top of the script so future you knows what it does.

Troubleshooting

If the script fails with an access-denied message, confirm the account has permission to the share and that the UNC path is correct. A path typo is a very common cause. If New-PSDrive reports that the drive already exists, remove the old mapping first or choose another letter. If the drive maps but does not appear in File Explorer, check whether you used -Persist; without it, the drive is session-only. If the script will not run at all, check the execution policy and whether the file is blocked (right-click the file, open Properties, and look for an Unblock option). If credentials are rejected, verify the user name format your network expects (domain\user or user@domain) and that the password has not expired. For persistent mappings that fail after a reboot, confirm the credential is available at logon; saved credentials sometimes need to be stored in Windows Credential Manager. When in doubt, test the share path with Test-Path before mapping, and ask your network administrator if permissions or group policy may be blocking drive mappings.

Frequently Asked Questions

Do I need administrator rights to map a drive? Not usually. Mapping a drive for your own user account typically works without elevation. Changing execution policy for all users or mapping for other accounts may require administrator rights.

What is the difference between New-PSDrive and net use? New-PSDrive is the PowerShell-native way and works well inside scripts. net use is the older command-line tool and is still useful for some legacy scenarios. Both can create persistent mappings.

Why does my mapped drive vanish after I close PowerShell? You probably omitted -Persist. Add it to keep the mapping across sessions and reboots.

Can I map a drive with a different username and password? Yes. Create a credential with Get-Credential and pass it to New-PSDrive using -Credential.

How do I remove a mapped drive? Use Remove-PSDrive -Name Z for the PowerShell mapping, or net use Z: /delete if it was created with net use. Persistent mappings may also need removal from File Explorer.

Does the script work on PowerShell 7 and Windows PowerShell? The core cmdlets are available in both, but some behaviors and default settings differ. Test your script in the version you plan to use.

PowerShell Script to Map Network Drive: Step-by-Step

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top