Edward Muldrew
Edward Muldrew

Reputation: 273

How to remove mapped network drive with Powershell?

I am trying to create and remove a new mapped network drive using PowerShell.

It creates the mapped drive, however I can't seem to remove the mapped drive. The error message I receive is:

Dir : Cannot find path 'C:\Windows\system32\P' because it does not exist.

New-PSDrive -Name "P" -Root "\\VM-Blue-Robin\Testing" -Persist -PSProvider "FileSystem" 
#Get-PSDrive P | Remove-PSDrive
#Remove-PSDrive -Name P -Force
#Remove-PSDrive P, Z

All Google and Stack Overflow has suggested to me thus far is using the commands that I have previously commented out. I am unsure of what I am doing wrong but had a feeling it could be done to the location of my files perhaps?

All help would be greatly appreciated!

Upvotes: 1

Views: 10451

Answers (1)

henrycarteruk
henrycarteruk

Reputation: 13207

The error is because you're running dir P instead of dir P: You need the : to signify a drive not a folder.

dir (which in Powershell is a actually an alias for Get-ChildItem) can read multiple areas of the OS so you need to be more specific with what you tell it.

Examples:

  • File system: Get-ChildItem C:
  • Registry: Get-ChildItem HKCU:
  • Certificate Store: Get-ChildItem cert:

Whilst with Get/Remove-PSDrive commands you are specifically telling it you want a "FileSystem" drive so it knows that Name is a drive letter.


With regards to removing the drive, either of the two commands you've listed will work fine:

New-PSDrive -Name P -Root "\\VM-Blue-Robin\Testing" -Persist -PSProvider "FileSystem" 

Get-PSDrive P | Remove-PSDrive
Remove-PSDrive -Name P -Force

Upvotes: 2

Related Questions