How Can I Create a Folder on the Start Menu?

How Can I Create a Folder on the Start Menu?

  • Comments 2
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I create a folder on the Start menu? I want the folder to be named the same as the %username% environment variable.

-- SB

SpacerHey, Scripting Guy! AnswerScript Center

Hey, SB. As we are wont to do, we’re going to break this question down into subtasks. We’ll explain how to do each of these subtasks, then at the end put the pieces together to create a single script that solves your problem for you.

To begin with, we need to figure out the name of the logged-on user (which happens to map to the %username% environment variable). That’s easy: we just create an instance of the WSH Network object and retrieve the value of the UserName property. That’s what we do here:

Set objNetwork = CreateObject("Wscript.Network")
strUser = objNetwork.UserName
strPath = "C:\Documents and Settings\" & strUser

Note that we’ve also created a variable named strPath and assigned it the value C:\Documents and Settings\ plus the user name (e.g., kenmyer). That gives us a path to a user folder like C:\Documents and Settings\kenmyer. If you want to create a shortcut to a different folder (for example, \\atl-fs-01\public\kenmyer), you just need to change this line of code accordingly.

Next we need to determine the locations of the Start menu Programs folder. That’s also easy. This time we create an instance of the WSH Shell object and call the SpecialFolders method, telling SpecialFolders we’d like the location of the Programs folder. We store the folder path in the variable strPrograms:

Set objShell = CreateObject("Wscript.Shell")
strPrograms = objShell.SpecialFolders("Programs")

(By the way, if you’d like to know the other folder paths you can locate using the SpecialFolders method, take a look at the “Working With Special Folders” section of the Microsoft Windows 2000 Scripting Guide.)

Finally we need to create the Start menu shortcut. Here’s the code that does that:

Set objShell = CreateObject("Wscript.Shell")
Set objShellLink = objShell.CreateShortcut(strPrograms & "\" & strUser & ".lnk")
objShellLink.TargetPath = strPath
objShellLink.Description = "Home folder for " & strUser
objShellLink.WorkingDirectory = strPath
objShellLink.Save

Here we again use the WSH Shell object, this time calling the CreateShortcut method. To create the Start menu shortcut we need to pass CreateShortcut the complete path to the shortcut file we want to create (for example, C:\Documents and Settings\kenmyer\kenmyer.lnk). To determine this path, we simply combine the path to the user folder (C:\Documents and Settings\kenmyer), a slash (\), the user name (kenmyer), and the file extension for a shortcut file (.lnk). That’s what we do with this line of code:

Set objShellLink = objShell.CreateShortcut(strPrograms & "\" & strUser & ".lnk")

Next we assign values to some of the shortcut file’s properties. The TargetPath represents the path to the folder; thus we assign it the path to the folder. The Description represents the tooltip that appears when you pause the mouse over the item in the Start menu; it gets a simple description indicating that it’s the home folder for the logged-on user. Finally, WorkingDirectory represents, well, the working directory for the shortcut; it gets assigned the path to the folder. After assigning these property values we call the Save method to save the shortcut file and add it to the Start menu.

Now, as promised, here’s a script that combines all three of our subtasks:

Set objNetwork = CreateObject("Wscript.Network")
strUser = objNetwork.UserName
strPath = "C:\Documents and Settings\" & strUser

Set objShell = CreateObject("Wscript.Shell")
strPrograms = objShell.SpecialFolders("Programs")

Set objShellLink = objShell.CreateShortcut(strPrograms & "\" & strUser & ".lnk")
objShellLink.TargetPath = strPath
objShellLink.Description = "Home folder for " & strUser
objShellLink.WorkingDirectory = strPath
objShellLink.Save
Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • <p>Why have none of the scripts been updated to work on all versions of Windows? &nbsp;Why aren&#39;t they created to be flexible enough to work in any of your own environments? (why does MS&#39; environment keep changing - not stable)? &nbsp;For instance, on Win 7 ... there is no &#39;Documents and Settings&#39; folder it is &#39;Users&#39;. &nbsp;Why not use an environ or Special folder to identify the proper structure based on the OS being run. &nbsp;Why make the example so basic they end up not being very useful? &nbsp;You have an opportunity to truly help and educate people, yet you deliver such mediocre tutorials. &nbsp;I expected much more!</p>

  • <p>@Daniel - There is no D &amp; S folder because most of us technicians badgered Microsoft to stop using dopey folder names.</p> <p>There is a very good tutorial on the links to the right. &nbsp;it will help you to understand why some of your concerns are not really a problem.</p> <p>In scripting we can retrieve the folder with an ID. &nbsp;This ID does not change from system to system. &nbsp;Environment variables are based on these IDs; &nbsp;%username%, %userprofile%, %alluserprofile%. &nbsp; &nbsp;These will always give you the correct location on any system. &nbsp;That is why they are there. &nbsp;In professional system admins may move these paths during an install and the variables will point to the new locations. &nbsp;It is how the OS is designed.</p> <p>In fact this design goes back to days before Windows. &nbsp;The same techniques were used on mainframes and mini systems. &nbsp;Unix and Mac use the same mechanisms.</p> <p>The information in the article is very simplistic. &nbsp;It is designed to teach some basic fundamentals. &nbsp;It is also very dated (2005). &nbsp;Try reading the info on the link to see what I mean.</p> <p><a rel="nofollow" target="_new" href="http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx">technet.microsoft.com/.../dd793612.aspx</a></p>