Learn about Windows PowerShell
Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to create a SendTo Notepad shortcut.
Microsoft Scripting Guy, Ed Wilson, is here. I absolutely love using Windows 8. To me, it is the best operating system we have ever released at Microsoft. I am especially proud that some of the Help articles I wrote are actually included with Windows PowerShell Help—it is so cool!
Anyway, one of my favorite things to do is to have a shortcut to Notepad in the SendTo special folder. This way, I can open and look at a lot of things without having to change file associations. I simply right-click, and select Notepad from the SendTo menu. In Windows 8, this is not a default location.
In the following image, I right-click the CreatePoshOrgPro… script, and point to Send to, but Notepad.exe is not listed as a default location to send items.
Ok, so I know that SendTo is a special folder. I guess I can open my user profile folder, find the SendTo folder and create a shortcut to Notepad there—I have done this literally dozens of times over the past few years. But when I attempt to do this, I get access denied. On closer inspection, the SendTo folder is not a real folder, but it redirects to another location.
Hmm … So I squirrel around trying to find where we hid the SendTo folder, and then I was reading over the text of my new Windows PowerShell 3.0 Step by Step book by Microsoft Press, and I ran across the section where I talked about using the old-fashioned WshShell object. It dawned on me that I could use this object to completely solve my problem. To begin with, the WshShell object is a COM object, so I need to create an instance of the object by using New-Object. I store the returned COM object in the $wshshell variable, as shown here.
$wshshell = New-Object -com wscript.shell
When I call the SpecialFolders property, I receive a listing of all the special folders about which the object knows. Here is the syntax I use.
The command and the output from the command are shown in the following image.
It is common that many COM objects have an item method. I decide to see if the SpecialFolders collection has one … if it does, I will feed it the specific SendTo folder. As shown here, the command works great.
11:08 C:\> $wshshell.SpecialFolders.Item("sendto")
I paste the path from the $wshshell.SpecialFolders.Item("sendto") command to the Windows Explorer path bar, and sure enough the SendTo folder opens. While I am there, I decide to delete a few things I never use.
I could go ahead and create a new shortcut here, but, dude, that would be like using the GUI and would be the first step to becoming a button monkey … somehow I cannot see myself doing that.
The SendTo folder is shown in the following image.
Well, it did not take much time to knock off a quick script. The first thing I need to do is to create the WshShell object and get the path to the SendTo folder—I had already written that code.
$path = $wshshell.SpecialFolders.Item("sendto")
Next, I need to create a path to use for the shortcut. I like to use Join-Path to create paths because it reduces possible concatenation errors.
$shortcutPath = Join-Path -Path $path -ChildPath "Notepad.lnk"
Now, I need to call the CreateShort method and pass the path for the shortcut. I have to store the returned object, because I need to specify a couple of properties. I put the object in the $shortcut variable.
$shortcut = $wshshell.CreateShortcut($shortcutPath)
Now, I need to specify the target—because Notepad is a well-known application I can avoid specifying the full path to the file. The description is not a requirement, but I like to fill out descriptions when they are available.
$shortcut.TargetPath = "Notepad.exe"
$shortcut.Description = "Created by Powershell 3.0"
Finally, I need to save my work and create the shortcut.
I mouse over to the SendTo folder, and cool it worked!
I uploaded the complete CreateNotePadShortCutInSendTo.ps1 script to the Scripting Guys Script Repository to make it easy for you to use the code.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
Better trick is to put a shortcut into the sendto folder that is a shortcut to the sendto folder. That way anything you want in the sendto folder you just use the shortcut in the sendto options to do so!
I would have just right clicked the SendTo (an obvious shortcut itself) and looked at the Link Properties in the SendTo Properties, which even has a nice little "Explore Target" button to push and open the link up.
Yes, that's not programmaticly the way to do so, but it works for me. :p
OK, you can call me a button monkey (but I can show you such command line treasures in my \BIN32 directory... but nevermind the now obsolete \BIN directory now that I'm 64-bit...)
@Phillip Malone --- hmmm that is a great idea. Had never thought of that before. Cool!!!
@Bryan Price When I right click on sendTo in the figure above, nothing shows up for the Target. When I attempt to double click it, I get access denied. It may be UAC keeping it from working for me. Obviously writing a script to do something that takes 3 seconds to do is not efficient -- however, if I decide that I want to add sendto to notepad for 500 workstations or a 1000 workstations via the logon script, then I have a great ROI story (as opposed to answering 500 or 1000 help desk calls). There is great value in knowing the GUI, and the Scripting Wife is MUCH better with the GUI than I am. Thanks for sharing (and for the record, I would NEVER call you a button monkey).
Genuine question .... what is wrong with the "open with" command ? same number of clicks ? re launching an old thread I know