Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

Weekend Scripter: Create a SendTo Notepad Shortcut

$
0
0

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.

Image of file directory

Finding the SendTo folder

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.

Image of Windows Explorer

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.

$wshshell.SpecialFolders

The command and the output from the command are shown in the following image.

Image of command output

It is common that many COM objects have an itemmethod. I decide to see if the SpecialFolders collection has one … if it does, I will feed it the specific SendTofolder. As shown here, the command works great.

11:08 C:\> $wshshell.SpecialFolders.Item("sendto")

C:\Users\ed.IAMMRED\AppData\Roaming\Microsoft\Windows\SendTo

So I have the path to the SendTo folder …

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.

Image of SendTo folder

Hey, I bet I can script it …

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.

$wshshell = New-Object -com wscript.shell

$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.

$shortcut.Save()

I mouse over to the SendTo folder, and cool it worked!

Image of Properties folder

CreateNotePadShortCutInSendTo.ps1

$wshshell = New-Object -com wscript.shell

$path = $wshshell.SpecialFolders.Item("sendto")

$shortcutPath = Join-Path -Path $path -ChildPath "Notepad.lnk"

$shortcut = $wshshell.CreateShortcut($shortcutPath)

$shortcut.TargetPath = "Notepad.exe"

$shortcut.Description = "Created by Powershell 3.0"

$shortcut.Save()

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 scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 


Viewing all articles
Browse latest Browse all 3333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>