INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Customizing PowerShell

Right click to run PowerShell PS1 Scripts by markdmac
Posted: 25 Apr 08 (Edited 9 May 11)

markdmac's Enterprise Ready Scripts
By Mark D. MacLachlan

Adding SendTo PowerShell Choice to Right Click Menus


By now you have started to tinker around with PowerShell and realize just how amazingly powerful this new scripting language is.  Perhaps you have even written a few scripts of your own or found some good sample ones you can use often.  The problem however is launching the scripts.  If you double click a PS1 script file it opens up in Notepad.  

Now if you are like me you have thought to yourself, "There has got to be a better way."

Well there is.  It is possible to launch a script from your GUI with a simple right click.  Below you will find both PowerShell and VBScript code.  Both scripts do the same thing.  Save either script and execute.  The PowerSHell will need to be run from a PowerShell prompt, the VBScript can just be double clicked.  Provided all works well, you will then have a choice of SendTo PowerShell.  This will let you right click a PS1 file and send it to PowerShell for execution.

Enjoy.

CODE


<#'==========================================================================
'
' NAME: SendToPowerShell.ps1
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' DATE  : 5/9/2011
' COPYRIGHT © 2011, All Rights Reserved
'
'    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
'    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
'    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'    PARTICULAR PURPOSE.
'
'    IN NO EVENT SHALL THE SPIDER'S PARLOR AND/OR ITS RESPECTIVE SUPPLIERS
'    BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
'    DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
'    WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
'    ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
'    OF THIS CODE OR INFORMATION.
'
' COMMENT: You are free to use this code provided you keep all original
'          header information in the script.
'          
'==========================================================================
#>


$SendTo = [System.Environment]::GetFolderPath("SendTo")
$OS = gwmi win32_OperatingSystem
$OSDir = $OS.WindowsDirectory.Trim()
$objShell = New-Object -com "Wscript.Shell"
if ($OS.OSArchitecture -eq "64-bit")
{
$objShortcut = $objShell.CreateShortcut($SendTo + "\PowerShell(x86).lnk")
$objShortcut.TargetPath = "$OSDir\SysWOW64\WindowsPowerShell\v1.0\PowerShell.Exe"
$objShortcut.Save()
}
$objShortcut = $objShell.CreateShortcut($SendTo + "\PowerShell.lnk")
$objShortcut.TargetPath = "$OSDir\System32\WindowsPowerShell\v1.0\PowerShell.Exe"
$objShortcut.Save()




CODE

'=============================================='
' NAME: SendToPowerShell.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: http://www.thespidersparlor.com
' DATE  : 4/24/2008
' COPYRIGHT (c) 2008 All Rights Reserved
'
' COMMENT: Adds SendTo right menu choice for PowerShell.
'
'    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
'    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
'    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'    PARTICULAR PURPOSE.
'
'    IN NO EVENT SHALL THE SPIDER'S PARLOR AND/OR ITS RESPECTIVE SUPPLIERS
'    BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
'    DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
'    WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
'    ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
'    OF THIS CODE OR INFORMATION.
'
'================================================
Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")
WinDir = WshShell.ExpandEnvironmentStrings("%WinDir%")
strSendToFolder = WSHShell.SpecialFolders("SendTo")

If Not objFSO.FolderExists(Windir & "\sysWOW64") Then
    strPathToNotepad = WinDir & "\system32\WindowsPowerShell\v1.0\powershell.exe"
    Set objShortcut = WSHShell.CreateShortcut(strSendToFolder & _
     "\PowerShell.lnk")
    objShortcut.TargetPath = strPathToNotepad
    objShortcut.Save
Else
    strPathToNotepad = WinDir & "\sysWOW64\WindowsPowerShell\v1.0\powershell.exe"
    Set ps86 = WSHShell.CreateShortcut(strSendToFolder & _
     "\PowerShell(x86).lnk")
    ps86.TargetPath = strPathToNotepad
    ps86.Save
    strPathToNotepad = WinDir & "\system32\WindowsPowerShell\v1.0\powershell.exe"
    Set ps64 = WSHShell.CreateShortcut(strSendToFolder & _
     "\PowerShell.lnk")
    ps64.TargetPath = strPathToNotepad
    ps64.Save
End If

Happy Scripting!

Back to PowerShell (Microsoft) FAQ Index
Back to PowerShell (Microsoft) Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close