How Can I Create a Shortcut in My Network Places?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I create a shortcut in My Network Places?

— KP

SpacerHey, Scripting Guy! AnswerScript Center

Hey, KP. You’ll have to excuse us if we sound a little out of breath; we’ve been running around like crazy looking for a complicated and highly-technical solution to this problem. It was only after we failed miserably in that quest that we began to think about what we were trying to do. Maybe we were trying to make this much harder than we needed to. After all, My Network Places is nothing more than a folder on the hard disk, and a “network place” is nothing more than a shortcut. Could creating a network place involve nothing more than creating a shortcut in a folder? You be the judge:

Const NETHOOD = &H13&

Set objWSHShell = CreateObject(“Wscript.Shell”)
Set objShell = CreateObject(“Shell.Application”)

Set objFolder = objShell.Namespace(NETHOOD)
Set objFolderItem = objFolder.Self
strNetHood = objFolderItem.Path

strShortcutName = “Finance Department Public Folder”
strShortcutPath = “\\atl-finance-01\public”

Set objShortcut = objWSHShell.CreateShortcut _
(strNetHood & “\” & strShortcutName & “.lnk”)
objShortcut.TargetPath = strShortcutPath
objShortcut.Save


We begin by defining a constant named NETHOOD and assigning it the value &H13&; we’ll use this in a moment to locate the My Network Places folder. We then create instances of two difference objects: the Windows Script Host Shell object and the Windows Shell object. That’s what these two lines of code do:

Set objWSHShell = CreateObject(“Wscript.Shell”)
Set objShell = CreateObject(“Shell.Application”)

Next we use a Windows Shell object method and property – Namespace and Self, respectively – to determine the location of the My Network Places folder. The Namespace method actually locates the folder for us; however, it returns a Folder object. For technical reasons we don’t need to worry about, we can’t do much with this object; therefore we use the Self property to return a FolderItem object representing the My Network Places folder. We do that because a FolderItem object is something that our script can deal with.


As soon as we have a FolderItem we can then grab the Path property and assign it to the variable strNetHood; strNetHood will now be equal to something along the lines of C:\Documents and Settings\kenmyer\NetHood. And, yes, that is a long and convoluted explanation for just three lines of code:

Set objFolder = objShell.Namespace(NETHOOD)
Set objFolderItem = objFolder.Self
strNetHood = objFolderItem.Path

Next we simply assign the name of the shortcut and the path to our network place to a pair of variables. We don’t really need to do this; we could just hard-code the values into the script. But assigning the values to variables will make it a little easier for you to modify the script to fit your own needs.


Now we turn to the WSH Shell object to actually create our shortcut. We begin by calling the CreateShortcut method, passing it the path to the new shortcut name. That will be a combination of four items: the path to the My Network Places folder (strNetHood), a trailing \, the name of our new shortcut (strShortcutName), and the file extension .lnk. That gives us a shortcut path similar to this:

C:\Documents and Settings\kenmyer\NetHood\Finance Department Public Folder.lnk

We set the TargetPath property of the shortcut to the network place (\\atl-finance-01\public) and then call the Save method to write the new shortcut to the file system. Just like that, we have a new network place.


Now, if you’ll excuse us, we need to go lie down for a moment. Who’d have thought that scripting would be such hard work?