A Guide to Mapping or Unmapping a Network Drive with PowerShell in Windows 11

|

|

The article details how to map or unmap network drives using the PowerShell application in Windows 11. It provides step-by-step instructions for the process, starting from opening the PowerShell app to entering the command format. The article also describes how to store credentials to avoid repeated password entry and how to disconnect shared network drives…

This article explains how to map or unmap network drives using PowerShell in Windows 11.

Windows PowerShell is a cross-platform task automation solution comprising a command-line shell, a scripting language, and a configuration management framework. The app is installed on Windows by default.

There are multiple ways one can map or unmap network drives in Windows. You can use File Explorer, the Command Prompt, or PowerShell app.

To map a network drive using PowerShell in Windows 11, you can follow these steps:

Open PowerShell by searching for it in the Start menu. Type the following command into the PowerShell: New-PSDrive -Name "DriveLetter" -PSProvider FileSystem -Root "\\Server\Share"

Replace “DriveLetter” with the letter, you want to assign to the network drive (e.g., Z) and “\\Server\Share” with the UNC path of the share you want to map (e.g., “\\myserver\myshare“).

That’s the basic PowerShell command format for mapping network shares in Windows.

Map or unmap network drive in PowerShell

As mentioned above, users have multiple ways to map or unmap (disconnect) network drives in Windows.

Usually, users will use File Explorer to map or disconnect a network drive. However, the PowerShell app can also map or unmap (disconnect) network drives.

Here’s how to do that.

First, open the PowerShell app by clicking on the Start Menu and searching for ‘PowerShell.’

Under Best match, select and open the PowerShell app. The Windows Terminal app will open with a PowerShell prompt.

Note: Do not run the terminal as an administrator to allow the mount to appear in File Explorer.

On the terminal window, type the command format as shown below:

New-PSDrive -Name "DriveLetter" -PSProvider "FileSystem" -Root "\\ServerName-or-IPAddress\SharedFolder" -Persist

Replace DriveLetter with a drive letter you want to use. Then change ServerName and SharedFolder to the server computer’s name and shared folder.

Example:

New-PSDrive -Name "Z" -PSProvider "FileSystem" -Root "\\HomePC\Documents" -Persist

If prompted for a username and password, type them correctly and press Enter to map the network share.

When you try to connect to a protected share, you will always get prompted to provide a username and password. To avoid typing your password, store it in a variable and pass it with the command.

If you want to include your username and password with the command, use the steps below.

First, run the command below to store your credentials in a variable.

$cred = Get-Credential -Credential USERNAME

Replace USERNAME with your account or username.

You should see a prompt window similar to the one below. Type in your password and click Ok to store your credentials.

Windows 11 use PowerShell to map network drives

With your credentials stored, run the command below to may the network drive and pass your credentials simultaneously.

New-PSDrive -Name "E" -Root "\\HomePC\Documents" -Persist -PSProvider "FileSystem" -Credential $cred

Alternatively, store your credentials in the Credential Manager. You can do that by running the commands below.

cmdkey /add:HomePC /user:WORKGROUND\username /pass:password

Replace WORKGROUP with your domain or workgroup name. The HomePC is the computer with the shared folders.

After storing your credentials in the Credential Manager, you can run the PowerShell command without specifying your credential or getting a prompt for your username and password.

New-PSDrive -Name "P" -Root "\\HomePC\Documents" -Persist -PSProvider "FileSystem"

Unmap or disconnect shared network drives using PowerShell

Once a shared drive is mapped, users can use PowerShell to unmap or disconnect it. Before you disconnect a shared network drive, you may want to list all shared network drives mapped to your computer.

Run the command below to do that.

Get-PSDrive -PSProvider "FileSystem"

Once you know the letter used to map the drive, run the commands below to unmap or disconnect it.

Remove-PSDrive -Name DriveLetter

Replace DriveLetter with the drive letter used to map the drive.

Ex.

Remove-PSDrive -Name Z

Disconnect multiple drives.

Get-PSDrive X, W | Remove-PSDrive

That should do it!

Conclusion:

This post showed you how to map or unmap (disconnect) shared network drives in Windows 11. If you find any errors above or have something to add, please use the comments form below.

Like this:



Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.