Workaround for "Network Drive Not connected" bug in Windows
Posted on June 21, 2020 • 2 minutes • 390 words
Table of contents
This post describes a work around to automatically connect network drives instead of showing the silly red X through them in explorer on Windows startup. It’s been a bug in Windows for about 15 years, and I believe it’s because of a longstanding race condition when the OS loads its services. In this blog article, we’ll do a little bit of powershell.
Symptoms
- In Windows Explorer, a red
X
appears over mapped network drives - Mapped network drives are displayed as Unavailable when you run the net use command at a command prompt
- In the notification area, a notification displays the following message:
Could not reconnect all network drives
Workaround
The following files should be executed on the regular command prompt - opposed to an elevated permissions command prompt to ensure they run with the same privilege as Windows Explorer.
- Create a script file named
MapDrives.cmd
Paste the following code into the new file:
1PowerShell -Command "Set-ExecutionPolicy -Scope CurrentUser Unrestricted" >> "%TEMP%\StartupLog.txt" 2>&1
2PowerShell -File "%SystemDrive%\Scripts\MapDrives.ps1" >> "%TEMP%\StartupLog.txt" 2>&1
- Create a script file named
MapDrives.ps1
Paste the following code into the new file:
1$i=3
2while($True){
3 $error.clear()
4 $MappedDrives = Get-SmbMapping |where -property Status -Value Unavailable -EQ | select LocalPath,RemotePath
5 foreach( $MappedDrive in $MappedDrives)
6 {
7 try {
8 New-SmbMapping -LocalPath $MappedDrive.LocalPath -RemotePath $MappedDrive.RemotePath -Persistent $True
9 } catch {
10 Write-Host "There was an error mapping $MappedDrive.RemotePath to $MappedDrive.LocalPath"
11 }
12 }
13 $i = $i - 1
14 if($error.Count -eq 0 -Or $i -eq 0) {break}
15
16 Start-Sleep -Seconds 30
17}
- Create a startup item
This workaround works only for devices that are already on the network at the time of log-on. If the device has not yet established a network connection by the time of logon, the startup script won’t automatically reconnect network drives. This script does not run continually in the background as that would take up computer resources unnecessarily.
- Copy the script file
MapDrives.cmd
to the following location:
1%ProgramData%\Microsoft\Windows\Start Menu\Programs\StartUp
- Copy the script file (MapDrives.ps1) to the following location:
1%SystemDrive%\Scripts\
Tip 🚀
You can quickly get to the above locations by opening up explorer and copy/pasting the above paths into the navigation bar.
Logs
A log file (StartupLog.txt) will be created in the %TEMP%
folder.
Finally, log off then back on again and you should correctly observe correctly mapped and accessible drives.