In today's interconnected world, it's essential to have a reliable and secure network connection. Windows users often need to switch between different networks based on their current tasks or active windows user. This article will guide you through the process of configuring your system to automatically switch active network connections based on the currently active Windows user.
Understanding the Basics
To accomplish this task, you'll need to create a script that detects the currently active user and sets the appropriate network accordingly. The following components are required:
- A PowerShell script to detect the active user and set the network
- A scheduled task to run the PowerShell script at login
Creating the PowerShell Script
First, create a PowerShell script (set-network.ps1) that checks the currently active user and sets the network accordingly:
$currentUser = (Get-WmiObject -Class Win32_ComputerSystem).UserName
if ($currentUser -eq "desired\_user\_1") {
Set-DnsClientServerAddress -InterfaceAlias "Ethernet" -ServerAddresses "192.168.1.1"
} elseif ($currentUser -eq "desired\_user\_2") {
Set-DnsClientServerAddress -InterfaceAlias "Ethernet" -ServerAddresses "10.0.0.1"
} else {
Write-Host "No matching user found."
}
Replace desired\_user\_1 and desired\_user\_2 with the actual usernames and 192.168.1.1 and 10.0.0.1 with the respective IP addresses of the DNS servers for each network.
Creating the Scheduled Task
Next, create a scheduled task to run the PowerShell script at login:
- Press Win + R and type
taskschd.mscto open the Task Scheduler. - Click Create Task under the Actions panel.
- In the General tab, set the name and description of the task.
- Under the Security options, select Run whether user is logged on or not and check Run with highest privileges.
- Switch to the Triggers tab and click New to create a new trigger. Choose At log on and set the appropriate settings for your use case.
- Move to the Actions tab and click New. Set the Action to Start a program, and in the Program/script field, enter
powershell.exe. In the Add arguments field, enter-ExecutionPolicy Bypass -File "C:\path\to\set-network.ps1". - Save the task and close the Task Scheduler.
By following these steps, you've created a system that automatically switches network connections based on the active Windows user. This setup ensures that you're always connected to the appropriate network for your tasks or users.