Powershell New-PSDrive Persist Between Login

new-psdrive credentials
new-psdrive -persist not working
new-psdrive not showing in explorer
new-psdrive remote computer
new-psdrive : the network path was not found
new-psdrive access is denied
new-psdrive : the network resource type is not correct
powershell map network drive on remote computer

I am using New-PSDrive to map network drives. My issue is that despite using the -Persist and -Scope Global parameters the drives no longer show up at the next Windows login for the user. The drives require credentials to access which I pass into the New-PSDrive command. I can see that the drives still exist after a logout and login by using net use at the command line, but the drives are not available. I believe what might be occurring is that Windows does not store the credentials and therefore cannot reconnect, but the drives do not show in Windows Explorer at all after logout and login. I am looking for a way to persist the drives as if I had created them through the "Map Network Drive" option in Windows File Explorer and checked the "Remember my credentials" option after selecting to use different credentials. Is this possible in Powershell? If not is there another Windows scripting language that would support this functionality? Here is my code:

#iterate through array of connection information and map drives
foreach ($connection in $connections)
{
    #split out array into variables
    $driveletter,$key,$account,$location = $connection

    $acctKey = ConvertTo-SecureString -String $key -AsPlainText -Force
    $credential = New-Object System.Management.Automation.PSCredential -ArgumentList $account, $acctKey
    New-PSDrive -Name $driveletter -PSProvider FileSystem -Root $location -Scope Global -Credential $credential -Persist -ErrorAction SilentlyContinue
}

My guess is that instead of New-PSDrive you'll need to use net use with the /PERSISTENT:YES and the /SAVECRED parameters in order to (a) create a persistent drive mapping that (b) also remembers the specified credentials (that's the aspect that New-PSDrive seems to be lacking).

Based on the code in your question, try something like:

net use $driveLetter $location $key /USER:$account /PERSISTENT:YES /SAVECRED

New-PSDrive, The drives require credentials to access which I pass into the New-PSDrive command. I can see that the drives still exist after a logout and login by using net use  PowerShell's Way of Mapping a Network Drive. The modern method for mapping a network drive is to use New-PSDrive. PowerShell 3.0 introduces the -Persist parameter so that we can not only see the name of your new drive in Windows explorer, but also know it's still there the next time we logon.

I have a similar issue with a customer.

Going through the documentation i found the following:

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.

Powershell New-PSDrive Persist Between Login, The New-PSDrive cmdlet creates temporary and persistent drives that are from the computer, Windows PowerShell automatically deletes the PSDrive that  The following features are added to New-PSDrive in Windows PowerShell 3.0: - Mapped network drives. You can use the Persist parameter of New-PSDrive to create Windows mapped network drives. Unlike temporary Windows PowerShell drives, Windows mapped network drives are not session-specific.

I was running across the same problem with a drive mapping script I had created. The credentials would not get saved and the drives disappeared from the File Explorer, but showed as unavailable in net use. It doesn't seem like PowerShell has a built-in method for accessing the Credential Manager. I should also mention that the drives I am mapping are in a different domain than the host machine and accessed via VPN.

For Credential Management, I pushed out this module to all of the workstations that needed to run this script: https://www.powershellgallery.com/packages/CredentialManager/2.0 That gives you the ability to save credentials to the Windows Credential Manager from within PowerShell.

I also created a custom WPF form that stills gets credentials securely (not relevant to the answer but I just needed more flexibility with the GUI).

This allowed me to get and save the credentials securely, however upon a reboot or logoff logon, the drives still disappeared from the File Explorer and showed as unavailable in net use.

Then I started thinking, what happens when you map a network drive via net use with /persist and /savecred or via File Explorer and tick the remember password box? So I checked the registry.

HKCU\Network\[drive letter]

There are 2 incorrect values that control how the File Explorer uses and shows mapped drives.

ConnectionType and DeferFlags.

I noticed that when mapping via New-PSDrive these 2 values were not being set correctly. So in my script after successfully mapping the drive and successfully saving credentials to the Credential Manager, I set ConnectionType to 1 and DeferFlags to 4.

Now after a reboot, the drives are persisted in the File Explorer and upon connecting to VPN and accessing the drives, the connections are restored with no need to enter a password. Hope this helps someone.

New-PSDrive, Syntax New-PSDrive [-name] string -PSProvider string [-Persist] [-root] string the mapping is permanently deleted from the computer, not just deleted from the​  I am using New-PSDrive to map network drives. My issue is that despite using the -Persist and -Scope Global parameters the drives no longer show up at the next Windows login for the user. The drives require credentials to access which I pass into the New-PSDrive command.

New-PSDrive - PowerShell, Solution: When you use the -Persist parameter to create a drive mapping (instead of a temporary PSDrive) you should specify the -Scope parameter as Global, This script is meant to run at Windows Login. Some info from the help file: Text. However, after I open a new PowerShell environment, and that my module is loaded: Get-PSDrive doesn't list my drive. ( I'm sure my module is loaded, I called some of its functions, I even re-imported with -Force , and Import-Module -Verbose doesn't show any errors )

[SOLVED] Powershell, New-PSDrive -Name A -Root \\company-server\SYSADMIN -PSProvider filesystem. I do have -Persist in the script, do I have it in the wrong place? I solved many of our intermittent drive mapping failures by having the logon script copy using .vbs for the scripts but you might try running the PowerShell script from there,  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 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.

[SOLVED] Drives unmap after powershell window closes, PowerShell 3.0 introduces the -Persist parameter so that the mapping is still there the next time we logon. PowerShell's Way of Mapping a Network Drive New-​PSDrive. The modern Key concept: this is a free tool from SolarWinds that analyzes network packets captured by Wireshark (also a free tool). New-PSDrive -Name S -PSProvider FileSystem -Root \\Address\Directory -Credential (Get-Credential) -Persist When I run it manually in a PS window, it persists. But if I save it into a .ps1 and call it, it doesn't persist. Bonus: if I put a start-sleep, or a read-host to pause the script after it's mapped, I can see the mounted drive.

Comments
  • Ever find a solution? Not saving the credentials on reboot has been leading to account lockouts.