Home » PowerShell » How to remove mapped network drive using PowerShell?

How to remove mapped network drive using PowerShell?

PowerShell Remove-PSDrive cmdlet used to remove mapped network drive in PowerShell and net use in cmd used to remove mapped network drive.

In this article, we will discuss how to remove mapped network drives using PowerShell and cmd in Windows System.

Using PowerShell to remove mapped network drive

Use the Remove-PSDrive cmdlet to remove mapped network drive using PowerShell.

Let’s consider we have mapped the network drive using New-PSDrive at drive letter K.

 New-PSDrive -Name "K" -PSProvider "FileSystem" -Root "\\Corp-in-18\Software" -Persist 

In the above PowerShell script, the New-PSDrive command creates a persistent mapped network drive K for the root specified “\Corp-in-18\Software” using the Root parameter.

To remove the mapped network drive use the following script.

Remove-PSDrive K -Force -Verbose 

VERBOSE: Performing the operation "Remove Drive" on target "Name: K Provider: Microsoft.PowerShell.Core\FileSystem
Root: K:\".

In the above PowerShell script, the Remove-PSDrive command removes the mapped network drive with the specified drive letter K. It uses the Force parameter to remove the drive forcefully.

To remove multiple drives, separate drives by comma (,) as given in the below command.

Remove-PSDrive J,K -Force -Verbose 

VERBOSE: Performing the operation "Remove Drive" on target "Name: J Provider: Microsoft.PowerShell.Core\FileSystem
Root: J:\".

VERBOSE: Performing the operation "Remove Drive" on target "Name: K Provider: Microsoft.PowerShell.Core\FileSystem
Root: K:\".

The following script uses the Get-PSDrive cmdlet to get the mapped network drive and pipes the result to the Remove-PSDrive to remove the specified drives.

Get-PSDrive J,K | Remove-PSDrive -Force -Verbose

VERBOSE: Performing the operation "Remove Drive" on target "Name: J Provider: Microsoft.PowerShell.Core\FileSystem
Root: J:\".

VERBOSE: Performing the operation "Remove Drive" on target "Name: K Provider: Microsoft.PowerShell.Core\FileSystem
Root: K:\".

Cool Tip: How to fix running the PowerShell script disabled on System!

Using Cmd to Remove Mapped Network Drive

Using net use command to remove mapped network drive by specifying drive letter.

To remove a single network drive, use the below command.

In the following script, the net use command removes a single network drive specified with the command, in this case, the K drive. The /delete option is used to specify the remove action.

C:\>net use K: /delete
K: was deleted successfully.

Cool Tip: Using a multiline command in PowerShell!

Conclusion

I hope the above article on removing mapped network drives using PowerShell and cmd is useful to you. PowerShell Remove-PSDrive cmdlet and net use in cmd used to remove mapped network drive.

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