Map Drive From Command Line | 2025 |
net use Z: \\server\share /user:DOMAIN\username MyPassword123 By default, a drive mapped via net use lasts only for the current user session. Log off, and it’s gone. To make a mapping persistent across reboots, add the /persistent:yes flag:
$cred = Get-Credential New-PSDrive -Name "Z" -PSProvider FileSystem -Root "\\server\share" -Credential $cred -Persist The Get-Credential dialog is secure, but for automation you can build a credential object (though storing passwords in scripts is still discouraged). PowerShell uniquely allows mapping a network share to a local folder path instead of a drive letter—something net use cannot do directly: map drive from command line
Next time you need to map a drive, don’t open File Explorer. Open Command Prompt or PowerShell—and feel the difference. PowerShell uniquely allows mapping a network share to
net use Z: \\server\share /persistent:yes Once you set /persistent:yes , subsequent net use commands (without specifying persistence) will also be persistent until you turn it off with /persistent:no . Sometimes you need to access a share with alternate credentials while logged into Windows with your standard account. The /savecred flag stores the password for future sessions: Sometimes you need to access a share with
New-PSDrive -Name "Z" -PSProvider FileSystem -Root "\\server\share" -Persist This creates a drive visible in File Explorer and across all applications—identical to net use Z: \\server\share . PowerShell handles credentials more securely using PSCredential objects:
net use Z: \\server\share /user:DOMAIN\username * The asterisk ( * ) tells Windows to prompt for a password without echoing it to the screen. For fully automated scripts (use with caution), you can include the password directly:
net use Z: \\server\share /user:OtherDomain\jsmith /savecred You will be prompted for the password once. After that, any script or command using that same mapping will reuse the stored credential—useful for scheduled tasks, but a security consideration. Network paths with spaces require quotation marks. Drive letters do not: