How Can I Create a Shortcut For an Executable File That Uses Parameters?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I create a shortcut where the target path is an executable file that uses parameters (for example, something.exe -t –s)?

— MC

SpacerHey, Scripting Guy! AnswerScript Center

Hey, MC. A week or so ago we mentioned that we were going to make some changes to Hey, Scripting Guy!; in particular, we were going to stop wasting time and start getting straight to the point. Needless to say, we didn’t exactly get around to that. Now we’re considering making a different change. You might have noticed that the layout for each of our Hey, Scripting Guy! columns includes a big Q for question and a big A for answer. That makes sense: after all, this is a question-and-answer column, something commonly referred to as “Q and A.” Except that Q and A is the way old fogeys describe this column, and the Scripting Guys would never want to be thought of as old fogeys. Therefore, we’re thinking about updating this column to A and A rather than Q and A, and replacing the big Q for question with a big A.

What’s the big A for? Well, for you old fogeys out there, the big A is for ask. You might be thinking, “Wait a second: ask is a verb, not a noun.” Well, that just shows how out-of-touch you are. At Microsoft (the company that consistently tries to reinvent the English language) the latest rage is to use the word ask as a noun; in particular, to use the word ask instead of using the word question. Here are a couple of examples drawn from emails the Scripting Guy who writes this column received the other day:

“I have an ask for everyone on this list.”

“Never mind; I discovered the answer to my own ask.”

OK, if you promise not to tell anyone, that sounds really weird to us, too. However, if that’s what it takes to sound cool, well, then that’s what it takes to sound cool. Therefore, MC, here’s an answer to your ask:

Set objShell = CreateObject(“Wscript.Shell”)
strDesktop = objShell.SpecialFolders(“Desktop”)

Set objShortcut = objShell.CreateShortcut(strDesktop & “\Test.lnk”) objShortcut.TargetPath = “Notepad.exe” objShortcut.Arguments = “C:\Scripts\Test.txt”

objShortcut.Description = “Starts Notepad with a file already loaded.” objShortcut.WorkingDirectory = strDesktop

objShortcut.Save

So how did we answer this ask? (You know, that still sounds kind of dorky to us.) To begin with, we created an instance of the Wscript.Shell object:

Set objShell = CreateObject(“Wscript.Shell”)

We then grabbed the path to the Desktop folder, something we do using the Shell object’s SpecialFolders property and the parameter Desktop:

strDesktop = objShell.SpecialFolders(“Desktop”)

Note. If you’re wondering why we grabbed the path to the Desktop folder, well, that’s easy: we have to put our shortcut somewhere, so we decided to put it on the Desktop.

From there we call the CreateShortcut method, passing the path to the Desktop folder and the value \Test.link as the method parameter:

Set objShortcut = objShell.CreateShortcut(strDesktop & “\Test.lnk”)

We probably don’t need to tell you that Test.lnk is the file name for our new shortcut file. But apparently we went ahead and told you anyway, didn’t we? Oh well.

For our little sample script we’re creating a shortcut that starts Notepad and then opens the file C:\Scripts\Test.txt. To that end, we assign Notepad.exe to the shortcut’s TargetPath property:

objShortcut.TargetPath = “Notepad.exe”

What’s that? Shouldn’t we have assigned Notepad.exe C:\Scripts\Test.txt to the TargetPath property? Well, you’d think so, wouldn’t you? However, that typically doesn’t work; in general, the target path needs to be a pointer to a specific file (or folder). In this case, that file is Notepad.exe; therefore, we need to set the TargetPath to Notepad.exe without including the parameter C:\Scripts\Test.txt.

So then where the heck do we put our parameter? That’s easy; we assign that value to the Arguments property:

objShortcut.Arguments = “C:\Scripts\Test.txt”

That’s where your command-line parameters go.

After that we assign values to the Description and WorkingDirectory properties. (Which we don’t have to do; these are optional properties.) We then call the Save method and officially create our new Desktop shortcut:

objShortcut.Save

And what do you think will happen when we double-click that shortcut? You already know what’s going to happen: Notepad is going to start up and, when it does, it’s going to load the file C:\Scripts\Test.txt. Which is just the sort of thing we wanted it to do.

OK. Having taken care of today’s ask we now have a tell for you. (Which is yet another example of Microsoft-speak. And, to be honest, we always thought that tell was a verb, too.) The Scripting Guys are still in Orlando, but that doesn’t mean cool things aren’t happening in the Script Center. For example, you still have plenty of time to win a Scripting Guys Gift Pack as part of our TechEd Challenge. We’ve got several new articles aimed at people interested in learning Windows PowerShell, and today we’re featuring part 2 of our two-part series on burning CDs with Windows Vista. If that doesn’t qualify as a tell, well, we don’t know what does.

Although, come to think of it, we don’t know what qualifies as a tell. Gee, maybe we are old fogeys after all.

0 comments

Discussion is closed.

Feedback usabilla icon