If you use a laptop for both work and personal use, you likely connect to multiple Wi-Fi networks throughout the day. When you arrive at the office and connect to the corporate Wi-Fi, you immediately need access to the company’s shared network drives. However, mapping a network drive permanently in Windows can cause annoying “Could not reconnect all network drives” errors when you take the laptop home and boot it up outside the corporate network.
Instead of manually mapping and unmapping the drive every day, you can use the Windows Task Scheduler to detect exactly which Wi-Fi network you just connected to, and automatically mount the network drive only when you are in the office.
Step 1: Create the Mapping Script
First, we need a simple batch script that tells Windows to map the drive. We will also include a command to delete any stale mappings before creating the new one, ensuring a clean connection.
- Open Notepad.
- Paste the following code into the document:
net use Z: /delete /y net use Z: \\ServerAddress\SharedFolder /persistent:no - Replace
Z:with the drive letter you want to use. - Replace
\\ServerAddress\SharedFolderwith the actual network path of your office drive. - Click File > Save As. Change the “Save as type” to All Files (*.*).
- Name the file
MapOfficeDrive.batand save it somewhere safe, like your Documents folder.
Step 2: Find Your Network Event ID
To trigger the script, we need Windows to listen for a specific event in the Event Viewer that occurs when you connect to Wi-Fi.
- Connect your laptop to the specific Wi-Fi network (e.g., your office Wi-Fi).
- Press the Windows Key, type Event Viewer, and open it.
- In the left pane, navigate to Applications and Services Logs > Microsoft > Windows > NetworkProfile > Operational.
- Look for a recent event with the Event ID 10000 (which signifies a successful network connection).
- Click on that event and look at the details pane below. Ensure the “Name” matches the name of your office Wi-Fi network.
Step 3: Automate with Task Scheduler
Now we link the Wi-Fi connection event to your batch script.
- Open the Start menu, type Task Scheduler, and launch it.
- In the right pane, click Create Task… (Do not use Basic Task).
- On the General tab, name the task “Map Office Drive” and check the box for Run with highest privileges.
- Switch to the Triggers tab and click New.
- Change “Begin the task” to On an event.
- Set the Log to Microsoft-Windows-NetworkProfile/Operational.
- Set the Source to NetworkProfile.
- Set the Event ID to 10000. Click OK.
- Switch to the Actions tab and click New.
- Ensure the action is set to Start a program, click Browse, and select the
MapOfficeDrive.batscript you created in Step 1. Click OK. - Switch to the Conditions tab. Check the box that says Start only if the following network connection is available, and select your specific office Wi-Fi network from the dropdown list.
- Click OK to save the task.
Your automation is now complete. Whenever you boot up your laptop at home, it will do nothing. But the exact second your laptop successfully authenticates and connects to your office Wi-Fi, the Z: drive will seamlessly appear in File Explorer.