How Can I Rename a Local Area Connection?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I rename a local area connection?

— AP

SpacerHey, Scripting Guy! AnswerScript Center

Hey, AP. You know, you hurt our feelings here: we Microsoft types spent a lot of time coming up with clever and catchy names like Local Area Connection, Local Area Connection 2, and Local Area Connection 3. And now you say you want to change those names? That hurts.


But you know what they say: the customer is always right. Give us a minute to dry our tears and then we’ll show you a script that changes the name of Local Area Connection 2 to Home Office Connection.


OK, we’re better now:

Const NETWORK_CONNECTIONS = &H31&

Set objShell = CreateObject(“Shell.Application”)
Set objFolder = objShell.Namespace(NETWORK_CONNECTIONS)

Set colItems = objFolder.Items
For Each objItem in colItems
If objItem.Name = “Local Area Connection 2” Then
objItem.Name = “Home Office Connection”
End If
Next


You might not be aware of this, but network connections are actually housed in a Windows special folder. Of course, this particular folder is really special: instead of having a path like C:\Documents and Settings\kenmyer\My Documents it has a path like this:

::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\::{21EC2020-3AEA-1069-A2DD-08002B30309D
}\::{7007ACC7-3202-11D1-AAD2-00805FC1270E}

Yikes! But that’s OK. You won’t have much success passing a folder path like that to the dir command, but you can use the Shell object to bind to the path, list all the items (that is, all the network connections) found in the folder, and rename any of those items. And that’s exactly what our script does.


We begin by defining a constant named NETWORK_CONNECTIONS and assigning it the value &H31&; we’ll use this constant to tell the Shell object which special folder we want to work with. We then create an instance of the Shell.Application object, then use the Namespace method to bind to the Network Connections folder. That’s what we do here:

Set objShell = CreateObject(“Shell.Application”)
Set objFolder = objShell.Namespace(NETWORK_CONNECTIONS)

After we bind to the Network Connections folder we can start doing something a bit more interesting. To begin with, we use the Items method to return a list of all the items found in the folder; because we’re dealing with the Network Connections folder that’s going to be a list of all the network connections on the computer. We then set up a For Each loop to iterate through the collection, checking to see if any of the connections have the Name Local Area Connection 2. If they do, we simply use the Shell object to change the Name to Home Office Connection. That’s what we do here:

For Each objItem in colItems
If objItem.Name = “Local Area Connection 2” Then
objItem.Name = “Home Office Connection”
End If
Next

That’s all you have to do: Local Area Connection 2 will now be named Home Office Connection and all will be right with the world.


And before you ask, the answer is no: we will never change the name of the Scripting Guys. Ever.


Well, unless – taking a cue from major league sports – someone would be interesting in buying the naming rights to the Scripting Guys. The Fabrikam Scripting Guys? Contoso Corporation Presents the Scripting Guys? We’re open to offers….