user1298258
user1298258

Reputation: 15

VB Script to map network location

I have a script that pretty much does what I want, except I would like it to save the shortcut to "%userprofile%\appdata\Roaming\Microsoft\Windows\Network Shortcuts" instead of the desktop. Thanks in advance for the help!

Set objShell=Wscript.CreateObject("Wscript.shell")
strDesktopFolder=objShell.SpecialFolders("Desktop") & _
"\"
Set objShortcut=objShell.CreateShortcut(strDesktopFolder & _
"Company Files.lnk")
objShortCut.TargetPath = "\\server\Folder"
objShortCut.Description = "Company Files Description"
objShortCut.Save

Upvotes: 1

Views: 5238

Answers (2)

Emperor Ming
Emperor Ming

Reputation: 11

This will Add a shortcut into the location you want and the location will show up in My Computer. However Scripts will not allow for the Folder location to show down the left explorer navigation menu like a network drive.

If you want to do this you will need to manually create a Folder Location by right clicking an empty space in My Computer and select "Add a Network Location" Once you have created the Network location right click on it and select Copy.

Save it somewhere on your server where you will store all of your network locations you want to script to peoples machines. Now in your script you will copy the network location folders from your server to "%userprofile%AppData\Roaming\Microsoft\Windows\Network Shortcuts"

This will add the location in My Computer and to the navigation pane just like a mapped drive.

This is the only way you can do this. The reason is a network location is no different than an actual File Folder. Just the path on the folder is pointing somewhere else. It is not a shortcut which is what all scripts will try to create when attempting this.

Here is a script if you just want a shortcut added in My Computer. Otherwise follow my other method.

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 = "Location Name"
strShortcutPath = "\\Server\Share"

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

Upvotes: 1

You can use objShell.ExpandEnvironmentStrings("%USERPROFILE%") to retreive the value from the %userprofile% environment variable.

Upvotes: 1

Related Questions