02 March, 2012

Configuring Wireless Settings from the Command Line or a Script

You can also configure wireless settings using commands in the netsh wlan context of the Netsh command-line tool, which enables you to create scripts that connect to different wireless networks (whether encrypted or not). To list available wireless networks, run the following command.
 
Netsh wlan show networks

Interface Name : Wireless Network Connection

There are 2 networks currently visible



SSID 1 : Litware

 Network Type  : Infrastructure

 Authentication  : Open

 Encryption  : None



SSID 1 : Contoso

 Network Type  : Infrastructure

 Authentication  : Open

 Encryption  : WEP
 
Before you can connect to a wireless network using Netsh, you must have a profile saved for that network. Profiles contain the SSID and security information required to connect to a network. If you have previously connected to a network, the computer will have a profile for that network saved. If a computer has never connected to a wireless network, you need to save a profile before you can use Netsh to connect to it. You can save a profile from one computer to an Extensible Markup Language (XML) file and then distribute the XML file to other computers in your network. To save a profile, run the following command after manually connecting to a network.
 
Netsh wlan export profile name="SSID"

Interface profile "SSID" is saved in file ".\Wireless Network

Connection-SSID.xml" successfully.

Before you can connect to a new wireless network, you can load a profile from a file. The following example demonstrates how to create a wireless profile (which is saved as an XML file) from a script or the command line.
 
Netsh wlan add profile filename="C:\profiles\contoso1.xml"

Profile contoso1 is added on interface Wireless Network Connection

To connect to a wireless network quickly, use the netsh wlan connect command and specify a wireless profile name (which must be configured or added previously). The following examples demonstrate different but equivalent syntaxes for connecting to a wireless network with the Contoso1 SSID.
 
Netsh wlan connect Contoso1

Connection request is received successfully

Netsh wlan connect Contoso1 interface="Wireless Network Connection"

Connection request is received successfully

Note that you need to specify the interface name only if you have multiple wireless network adapters—an uncommon situation. You can use the following command to disconnect from all wireless networks.
 
Netsh wlan disconnect



Disconnection request is received successfully

You can use scripts and profiles to simplify the process of connecting to private wireless networks for your users. Ideally, you should use scripts and profiles to save users from ever needing to type wireless security keys.
You can also use Netsh to allow or block access to wireless networks based on their SSIDs. For example, the following command allows access to a wireless network with the Contoso1 SSID.
 
Netsh wlan add filter permission=allow ssid=Contoso networktype=infrastructure

Similarly, the following command blocks access to the Fabrikam wireless network.
 
Netsh wlan add filter permission=block ssid=Fabrikam networktype=adhoc

To block all ad hoc networks, use the Denyall permission, as the following example demonstrates.
 
Netsh wlan add filter permission=denyall networktype=adhoc

To prevent Windows from automatically connecting to wireless networks, run the following command.
 
Netsh wlan set autoconfig enabled=no interface="Wireless Network Connection"

You can also use Netsh to define the priority of user profiles (but not Group Policy profiles). Group Policy profiles always have precedence over user profiles. The following example demonstrates how to configure Windows to connect automatically to the wireless network defined by the Contoso profile before connecting to the wireless network defined by the Fabrikam profile.
 
Netsh wlan set profileorder name=Contoso interface="Wireless Network Connection"

priority=1

Netsh wlan set profileorder name=Fabrikam interface="Wireless Network Connection"

priority=2

Netsh has many other commands for configuring wireless networking. For more information, run the following at a command prompt.
 
Netsh wlan help

Note

When troubleshooting problems connecting to wireless networks, open Event Viewer and browse the Applications And Services Logs\Microsoft\Windows\WLAN-AutoConfig event log. You can also use this log to determine the wireless networks to which a client is connected, which might be useful when identifying the source of a security compromise.

Windows Batch Script - Getting Script Location (Directory/Path)

Inside a Windows batch file (.bat or .cmd) you can reference the drive and folder where the batch file is located by: %~D0
CHDIR %~DP0
The first line causes the script to change to the drive where the script is located.
The second line causes the script to change to the directory where the script is located.
See Call command for further information.

Windows 7 Bug

Windows 7 has a bug in the handling of %~dp0 and %~f0 which can cause this to fail. When running a .cmd or .bat script and using %~f0 or %~dp0 to resolve the batch script name if the batch script name has spaces (ex: "my script.cmd") and you chdir within the script then further references to %~dp0 will incorrectly resolve to the new cwd instead of the correct batch script location.
This does not happen if the script name has no spaces in it.
Example script below:
@REM When this script is named "test.cmd" (no spaces) it works as expected for the value of %~f0

@REM When this script is named "test with spaces.cmd" it does not work

echo "Stage 1:  dp0 == %~dp0"

SET STAGE1=%~dp0


%~d0
cd "%~dp0"
mkdir TestSubFolder
cd TestSubFolder

echo "Stage 2:  dp0 == %~dp0"
echo Should have gotten value %STAGE1%

Work Around

I've filed a bug with Microsoft at https://connect.microsoft.com/PowerShell/feedback/details/617705/windows-shell-bug-with-how-dp0-is-resolved. In the mean time if you make the FIRST statements in your batch as follows you can still use this trick:
@REM Must set these first thing due to bug in Windows 7 when batch script filename has spaces in it
SET BATCH_SCRIPT_DRIVE=%~d0
SET BATCH_SCRIPT_FOLDER_PATHNAME=%~dp0
You could then do
%BATCH_SCRIPT_DRIVE%
CD "%BATCH_SCRIPT_FOLDER_PATHNAME%"
to make the current working drive and directory the same as your scripts.