12

I am running a Windows Server 2012 installation inside VMWare Workstation. I have shared folders enabled, so drive Z: is mapped to \\vmware-host. I can correctly access this drive in Explorer (I can access Z: inside the "normal" `cmd) and I can properly open files.

I opened PowerShell and tried to to cd Z: and I got the following error:

cd : Cannot find path 'Z:\' because it does not exist.

Why can't PowerShell find the already mapped network drive?

I Googled a bit, and found the New-PSDrive command. I ran the following:

New-PSDrive -Name 'Z' -PSProvider FileSystem -Root 'Z:\'

but I still get the same error as above. What do I need to do to access the VMWare shared folders inside PowerShell.

gen_Eric
  • 211
  • 1
  • 5
  • 17
  • What does `Get-PSDrive -PSProvider FileSystem` report when you open a new Powershell console? – jscott Mar 05 '15 at 20:35
  • @jscott: In a new console, I see `A`, `C`, and `D`. In the console I already have open, I also see `Z`. Why does the new console not show `Z`? – gen_Eric Mar 05 '15 at 20:39
  • 4
    Are you running Powershell elevated? If so, please try running it without "Run as administrator". You would need to re-map the drive if running elevated. http://stackoverflow.com/questions/4742992/cannot-access-network-drive-in-powershell-running-as-administrator – jscott Mar 05 '15 at 20:42
  • @jscott: It's running as Administrator. I also ran `Set-ExecutionPolicy -ExecutionPolicy Unrestricted`. I'm using PowerShell to try to run a script to call `New-ADUser`. – gen_Eric Mar 05 '15 at 20:43
  • @jscott: I just ran PowerShell as my user (not Administrator), and it was able to access `Z:` with no issues. – gen_Eric Mar 05 '15 at 20:44
  • See my link above. The elevated console would not have access. – jscott Mar 05 '15 at 20:44
  • @jscott: Yep. That's what's going on here! Thanks a lot :) – gen_Eric Mar 05 '15 at 20:46

3 Answers3

12

You need to make the drive mapping available to the user you're running Powershell as. You can do the following to give that user the same drive mapping:

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

See the following SO question: Cannot access network drive in PowerShell running as administrator

jscott
  • 24,484
  • 8
  • 79
  • 100
  • May I also mention that Powershell supports the use of PUSHD and POPD (or more formally : push-location and pop-location) which may be more suitable for temporarily mapping a drive for a specific operation. – Get-HomeByFiveOClock Mar 06 '15 at 00:30
7

Are you starting this PowerShell console as Administrator or as your current user?

If you are running it from a non-elevated command prompt, then it should see all your locally mapped network drives. I'm in the same situation as you right now and have x: mapped to \\vmware-host\[folder], and a Get-PSDrive -PSProvider FileSystem shows me all my locally mapped drives.

However, if I start this console as an Administrator, it does not see any of my local mapped drive, as for an elevated command prompt a different (second) security context is used to load the application, which does not share network connections.

The good news is that you basically just need to map the drive in the administrator console just once and it will remain in that security context (until a reboot, I think).

If you want to automate this, you should read up on auto-loading scripts when you start a powershell session. Basically if you create a folder in your My Documents called WindowsPowerShell and inside that create a Microsoft.PowerShell_profile.ps1 file, it will trigger every time you start a new powershell console. There you can add a net use command, or add custom functions that you've written that you want to use.

For example, I have one that turns my cursor red for administrative consoles, and has a function that I can easily call to sign my PS1 scripts.

Mark Henderson
  • 68,823
  • 31
  • 180
  • 259
  • Yeah, that's what was going on here. The drive was mapped as my user, and the PowerShell was running as Administrator. I'm used to Linux shells, not PowerShell. – gen_Eric Mar 05 '15 at 20:49
  • That's a really good tip about the admin console prompt. What do you use in your profile.ps1 to detect if the session is elevated or not? – stib Jul 12 '17 at 09:36
  • Adding to @mark's answer I recommend a function for net use in $profile: `function map_net {net use z: $wsl }` that you can call whenever you need the drive in the admin console. – Timo Oct 25 '20 at 09:01
0

To get that New-PSDrive command to work, you need to use a UNC path for -Root instead of "Z:", since elevated PowerShell doesn't know about Z: yet:

New-PSDrive -Name 'Z' -PSProvider FileSystem -Root '\\server\sharedFolder'

My understanding is that jscott's answer (net use...) works in more cases, though.

Vimes
  • 101
  • 1