How to Automatically Map Network Drives in Windows 11 using PowerShell

The Shift from Batch Scripts to PowerShell

For decades, IT administrators mapped network drives in Windows using simple Logon Batch scripts utilizing the net use command (e.g., net use Z: \\server\share). While this legacy command still works in Windows 11, it is increasingly unreliable over VPN connections, struggles with modern Azure Active Directory authentication, and lacks robust error handling.

Microsoft now recommends managing mapped network drives via PowerShell. Using the New-PSDrive cmdlet provides significant advantages: you can embed credentials securely, specify exactly how the drive should persist across reboots, and log errors cleanly if the server is unreachable.

Understanding the New-PSDrive Cmdlet

PowerShell manages drives using “providers.” While you usually think of a drive as a physical disk, PowerShell can treat the registry, certificates, and network shares as “drives.”

To map a network share to a drive letter in Windows File Explorer, we must use the FileSystem provider and include the -Persist flag.

Step-by-Step: Mapping a Network Drive

Example 1: The Basic Persistent Drive Map

If you simply want to map the Z: drive to a network share at \\FileServer01\Marketing, and you want this drive to remain after you reboot the computer, open PowerShell and run:

New-PSDrive -Name "Z" -PSProvider "FileSystem" -Root "\\FileServer01\Marketing" -Persist

Breakdown of the command:

  • -Name “Z”: The drive letter to assign (do not include the colon).
  • -PSProvider “FileSystem”: Tells PowerShell this is a standard file storage drive.
  • -Root: The UNC path to the network share.
  • -Persist: Crucial flag. Without this, the drive will only exist within that specific PowerShell session. With it, the drive appears in Windows File Explorer and reconnects on reboot.

Example 2: Mapping a Drive with Alternate Credentials

Often, you need to connect to a share using a different username than the one you used to log into Windows 11. New-PSDrive handles this elegantly using the -Credential parameter.

$credentials = Get-Credential
New-PSDrive -Name "M" -PSProvider "FileSystem" -Root "\\FileServer01\Management" -Credential $credentials -Persist

When you run this script, Windows will securely prompt the user with a standard authentication popup to enter their username and password before mapping the drive.

How to Remove a Mapped Drive

If you need to unmap a drive, do not use the legacy net use Z: /delete command if you created it with PowerShell. Instead, use the Remove-PSDrive cmdlet.

Remove-PSDrive -Name "Z"

If the drive throws an error stating it is in use, you can force the removal:

Remove-PSDrive -Name "Z" -Force

Automating Drive Mapping via Logon Scripts

If you want to automate this process so drives map silently every time a user logs in, you can save your command into a .ps1 script file (e.g., MapDrives.ps1).

Because Windows 11 restricts the execution of unsigned PowerShell scripts by default, you cannot simply place the script in the Startup folder. Instead, you must use the Windows Task Scheduler to trigger the script.

  1. Open Task Scheduler.
  2. Click Create Basic Task….
  3. Name it “Map Network Drives”.
  4. Set the Trigger to When I log on.
  5. Set the Action to Start a program.
  6. In the Program/script box, type: powershell.exe
  7. In the Add arguments box, type: -WindowStyle Hidden -ExecutionPolicy Bypass -File "C:\Scripts\MapDrives.ps1"

Conclusion

Transitioning from legacy net use batch files to the PowerShell New-PSDrive cmdlet provides a much more robust and manageable way to handle network storage in Windows 11. By utilizing the -Persist and -Credential flags, administrators can ensure secure, reliable connections to file servers across modern enterprise environments.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.