PoshCode Logo PowerShell Code Repository

Adds ‘Execute in PowerShell’ options to .ps1 files’ context menu so that you can easily run scripts from Windows Explorer

  1. #===================================================================================
  2. #
  3. #       Filename:       AddExecuteInPowerShellToPS1Files.ps1
  4. #
  5. #       Author:         Nigel Boulton
  6. #
  7. #       Version:        1.00
  8. #
  9. #       Date:           9 Nov 2008
  10. #
  11. #       Mod dates:     
  12. #
  13. #       Purpose:        To add context menu items for .ps1 files in Windows Explorer, to
  14. #                               allow PowerShell scripts to be executed by right-clicking them
  15. #
  16. #       Action:         Gets the path to PowerShell.exe by reading the Path property of
  17. #                               the process. If PowerShell is not running (e.g. if using a GUI/IDE),
  18. #                               prompts the user to     enter the path. Quotes the path if it contains
  19. #                               any spaces
  20. #                               Gets the class key for .ps1 files from the registry and adds the
  21. #                               appropriate subkeys and values to it to add "Execute in PowerShell"
  22. #                               and "Execute in PowerShell (-NoExit)" context menu items
  23. #
  24. #       Notes:          USE AT YOUR OWN RISK! Backup your system first and all that...
  25. #                               Must be run as an administrator
  26. #
  27. #===================================================================================
  28.  
  29. # Get the full path to PowerShell.exe, or prompt if using a GUI
  30. $PoSH = Get-Process | Where-Object {$_.Name -eq "PowerShell"} | Select-Object -First 1
  31. If ($PoSH -eq $null) {
  32.         $PoSHPath = Read-Host "Please enter the full path to PowerShell.exe, or cancel and run this script in a PowerShell console session"
  33.         If ($PoSHPath -eq $Null) {exit 1}
  34.         }
  35. Else
  36.         {
  37.         $PoSHPath = $PoSH.Path
  38. }
  39.  
  40. # Quote PowerShell path if it isn't already
  41. If ($PoSHPath.Substring(0,1) -ne "`"") {$PoSHPath = "`"" + $PoSHPath}
  42. If ($PoSHPath.Substring($PoSHPath.Length - 1,1) -ne "`"") {$PoSHPath += "`""}
  43.  
  44. # Get class for .ps1 files
  45. $PS1Class = (Get-ItemProperty -Path HKLM:SOFTWARE\Classes\.ps1)."(Default)"
  46.  
  47. # Build hash table of registry data to add
  48. # $_.Name is the registry key and $_.Value is the data for the "(Default)" value in that key
  49. $RegData = @{"HKLM:Software\Classes\$PS1Class\shell\Execute in PowerShell\command" = $PoSHPath + " &'%1'";`
  50.                          "HKLM:Software\Classes\$PS1Class\shell\Execute in PowerShell (-NoExit)\command" = $PoSHPath + " -NoExit &'%1'"}
  51.  
  52. # Check for an existing "Open" action. If there isn't one we need to add one in to ensure that "Open"
  53. # remains the default. If we didn't do this, in future double-clicking .ps1 files would execute them!
  54. If ((Test-Path "HKLM:Software\Classes\$PS1Class\shell\open\command") -eq $False) {$RegData.Add("HKLM:Software\Classes\$PS1Class\shell\open\command", "notepad.exe `"%1`"")}
  55.  
  56. # Iterate hash table to create registry keys and set values
  57. $RegData.GetEnumerator() | ForEach-Object {
  58.         # Create registry path
  59.         If ((Test-Path $_.Name) -eq $False) {New-Item -Path $_.Name -Force | Out-Null}
  60.         # Set "(Default)" value
  61.         Set-ItemProperty -Path $_.Name -Name "(Default)" -Value $_.Value
  62. }
  63. Write-Output "Processing complete"

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me