Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Right click to run PowerShell PS1 Scripts

Customizing PowerShell

Right click to run PowerShell PS1 Scripts

by  markdmac  Posted    (Edited  )
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:
[green]
<#'==========================================================================
'
' 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.
'          
'==========================================================================
#>[/green]

$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!
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top