79

I'm running PowerShell in a Windows 7 x64 virtual machine. I have a shared folder on the host mapped as a network drive (Z:). When I run PS normally I can access that drive just fine, but if I run it "as administrator" it tells me:

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

How do I access network drives as administrator?

Segfault
  • 8,036
  • 3
  • 35
  • 54
EMP
  • 59,148
  • 53
  • 164
  • 220
  • 3
    Related: http://stackoverflow.com/questions/1267085/vista-uac-trouble-mapping-network-drives. Looks like the options are a registry change or remapping the drive in the elevated process. – ig0774 Jan 20 '11 at 02:55
  • 1
    I tried the registry hack and that didn't help, but remapping the drive in an elevated process did - thanks. You should post that as an answer. – EMP Jan 20 '11 at 03:22

7 Answers7

113

In the end the fix was simply to re-map the drive letter while running as Administrator:

net use Z: "\\vmware-host\Shared Folders"

It doesn't have to be done from the same PowerShell instance (or from PowerShell at all) - it's just something that needs to be done once for the entire logon session.

EMP
  • 59,148
  • 53
  • 164
  • 220
  • 3
    A handy way to do that without leaving the shell is to use `runas`: `runas /user:administrator net use Z: "\\vmware-host\Shared Folders"` – andersonvom Mar 06 '12 at 23:25
  • 2
    Works for me. And for other ``net use`` params like ``/persistent`` over next logon, check Microsoft net use [documentation](http://technet.microsoft.com/en-us/library/gg651155(v=ws.10).aspx) – Casey May 29 '13 at 15:27
  • If you need the drive to be visible to user accounts other than yourself, e.g. scheduled tasks/services, try [my answer](http://stackoverflow.com/a/26579901/33080) - which also makes the drive visible to you whether you're elevated or not. – Roman Starkov Apr 06 '17 at 00:07
  • For VirtualBox the path is `\\vboxsvr\share`, works like a charm as well. – Jaded Feb 24 '20 at 13:32
  • Net use works and new-psdrive does not work is WHAT I LEARNED;) – Timo Oct 25 '20 at 08:58
11

In my case, I was able to simply use the UNC path instead of the drive mapping and it worked fine.

So, per your example, instead of using the mapped drive Z:\, I just used "\\vmware-host\Shared Folder" as the path.

Timothy Lee Russell
  • 3,719
  • 1
  • 35
  • 43
7

One other work-around that took me ages to find is to run net use from a scheduled task as the NT AUTHORITY\SYSTEM account. Apparently drives mapped under this account show up for all users and all elevation levels.

I've tested this and it works even on NFS shares (which can be a bit finicky). Just create a scheduled task set to run at system startup, and specify the usual command:

net use Z: \\server\share /persistent:no

It might possibly work to run it just once with /persistent:yes, but I haven't tried that. Granted, "just map it again" works too, but that drive still won't be visible to scheduled tasks running in different contexts. The downside is that all real users see it too, so not so good for multiuser setups.

ysalmi
  • 529
  • 4
  • 16
Roman Starkov
  • 59,298
  • 38
  • 251
  • 324
4

I'm using the following hacky solution where I recreate "missing" PSDrives in profile.ps1 when Powershell is running in elevated mode.

Gist

# Reconnect PSDrives for network connections when running with elevated privileges
$elevated = (([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
if( $elevated ) {
    net use | ?{ $_ -match ":\s+\\\\"  -and !$_.StartsWith("Unavailable") } | %{
        $tokens = $_.split(":")
        $psdrivename = $tokens[0][$tokens[0].length-1]
        $path = $tokens[1].trim().split(" ")[0].trim()

        if( !(get-psdrive | ?{ $_.Name -eq $psdrivename } )) {
            write-host ( "Restoring PSDrive for {0}: {1}" -f $psdrivename, $path )
            new-psdrive $psdrivename FileSystem $path | out-null
        }
    }
}  
0

Seems like a known problem to Microsoft since Vista.
Microsoft Knowled base article with unsafe registry fix.

We are currently evaluating this approach as some of our guys have feelings that machine may not start after this ;-)

Casey
  • 1,402
  • 1
  • 19
  • 23
0

How about mapping a new psdrive to acess that data? PSDrives work just as well if not better than system mapped drives when you are writing scripts or accessing network data stores in powershell.

Instructions for using the New-PSDrive cmdlet are here: Technet:New-PSDrive

If you don't want to have to make a new psdrive every time you could add it to the profiles for both the Administrator and your user account and it will automatically be available every time you open powershell.

~Dan

thoughtpunch
  • 1,907
  • 4
  • 25
  • 41
0

None of the other answers worked for me; but @TimothyLeeRussell's answer pointed me in the right direction.

In my case, I had a .bat file located on a network drive. When I ran it as administrator it would just flash a command prompt window and instantly disappear; it worked fine when I ran the file's contents from an elevated command prompt.

Finally I realized that I tried running the .bat file from the mapped network drive. I changed the execution of the file to use the UNC path and it worked.

Lews Therin
  • 3,707
  • 2
  • 27
  • 53