Set-Location : Cannot find drive. A drive with the name 'P' does not exist.

Hello All,

I have a strange Problem with network drives I am mapping and using in Powershell. If I map a network drive the first time I could set-location and able to browse thought the drive. If I dissconect the drive and re-create the drive again, I am not able to set-location and browse to the drive anymore...

$PDrive = "\\ShareName\Projects$\P1757_xxx"
(new-object -com WScript.Network).MapNetworkDrive("P:",$PDrive)
Set-Location P: WORKING :-)
Set-Location C:
(new-object -com WScript.Network).RemoveNetworkDrive("P:")

$PDrive = "\\ShareName\Projects$\P1757_xxx"
(new-object -com WScript.Network).MapNetworkDrive("P:",$PDrive)
Set-Location P: NOT WORKING :-(

Set-Location : Cannot find drive. A drive with the name 'P' does not exist.
At line:1 char:13
+ Set-Location <<<<  P:
    + CategoryInfo          : ObjectNotFound: (P:String) [Set-Location], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SetLocationCommand

Share is showing up in PsDrive and on net use, but I Am Not able to use it thought PowerShell. Same Problem if I Map and Remove the NetworkDrive with net use... In CMD the Network Drive is accessiable after Recreation?????

November 3rd, 2011 4:46pm

Try setting "UpdateProfile" to true:

(new-object -com WScript.Network).MapNetworkDrive("P:",$PDrive,"true")

From http://msdn.microsoft.com/en-us/library/8kst88h6(v=VS.85).aspx

Free Windows Admin Tool Kit Click here and download it now
November 3rd, 2011 5:10pm

Hi Karl,

have tried your suggestion, but with no luck. Found out, that the Problem occures also on other Systems and seem to be a Bug aor anything else...

Have tryed the following...

$PDrive = "\\Servername\Projects$\_Archive\Projekt _Archiv\P1754_WSS-BO"
(new-object -com WScript.Network).MapNetworkDrive("P:", $PDrive, $true)
Set-Location P:
ls
Set-Location C:
(new-object -com WScript.Network).RemoveNetworkDrive("P:", $true, $true)

Most time it works, and sometime it breaks. ????

November 4th, 2011 11:14am

That's very odd - I never have problems here - but then, I don't map a drive and then unmap it...

Karl

Free Windows Admin Tool Kit Click here and download it now
November 4th, 2011 3:08pm

I've notice that if you try to remap drive which was already mapped earlier (not on current PS session) it works fine.

So workarround for this issue could be to run task via two scripts:

  • One - which will do intial drive mapping and initiates second sript.
  • Second - which will do actual job with drive remapping 

July 16th, 2013 7:44pm

I have a script that runs daily that maps a drive 90 times using the same drive letter, without any problem.

One thing sticks out from your script is that you create a new Wscript.Network object every time you map/unmap drives.  This is totally unnecessary and maybe the cause of the problem.  Just create a variable to hold the com object and keep reusing it.  At the end of the script just dispose the com object to release the memory.

Also, set the "updateprofile" parameter to $false.  This has the same effect as /persistent:no in net use.
Free Windows Admin Tool Kit Click here and download it now
July 16th, 2013 10:35pm

I have a similar Problem, but still no idea where to search.

When i use after the 20th mapping of a pci the cgi command to search for some files i get the same message.

But it seems the mapping is successful.

I can see the drive an i am also able to read from some absoulut file path file informations.

Only CGI returns this error.


September 3rd, 2015 3:43am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics