Petr Synek
Petr Synek

Reputation: 135

Mount permanent drive in powershell

Is it possible to mount a drive in powershell permanently? Even after system restart?

When I use New-PSDrive -Persist (and all other required parameters of course), it works only for the current windows session. I use PS 5.1

I can see the drive in file explorer, it is persistent in the system, but I don't see it from Powershell ISE, even if run by administrator. I have to use New-PSDrive to be able to load a script from a network drive, when I need to work on it. And whenever I reboot the computer, the drive once again can't be seen from the ISE. It's kind of annoying having to do it again and again all the time.

Upvotes: 3

Views: 11314

Answers (2)

xjcl
xjcl

Reputation: 15329

For me the command

New-PSDrive -Name "X" -Root "\\some\path" -PSProvider "FileSystem" -Persist

persisted the drive only until reboot. Running net use instead fixed this:

net use x: \\some\path /persistent:Yes

So these commands seem very much not equivalent. I am on some weird corporate VPN though so YMMV.

Upvotes: 1

Maximilian Burszley
Maximilian Burszley

Reputation: 19684

Your understanding of the -Persist switch is incorrect. As noted in the help, make sure you're specifying global scope.

New-PSDrive -Name 'X' -PSProvider 'FileSystem' -Root '\\share\C$' -Scope 'Global' -Persist

-Persist [<SwitchParameter>]

Indicates that this cmdlet creates a Windows mapped network drive. Mapped network drives are saved in Windows on the local computer. They are persistent, not session-specific, and can be viewed and managed in File Explorer and other tools.

When you scope the command locally, that is, without dot-sourcing, the Persist parameter does not persist the creation of a PSDrive beyond the scope in which you run the command. If you run New-PSDrive inside a script, and you want the new drive to persist indefinitely, you must dot-source the script. For best results, to force a new drive to persist, specify Global as the value of the Scope parameterin addition to adding Persist to your command.

The name of the drive must be a letter, such as D or E. The value of Root parameter must be a UNC path of a different computer. The value of the PSProvider parameter must be FileSystem.

To disconnect a Windows mapped network drive, use the Remove-PSDrive cmdlet. When you disconnect a Windows mapped network drive, the mapping is permanently deleted from the computer, not just deleted from the current session.

Mapped network drives are specific to a user account. Mapped network drives that you create in sessions that are started by using the Run as administrator option or by using the credential of another user are not visible in a session that was started without explicit credentials, or by using the credentials of the current user.

Upvotes: 1

Related Questions