216

I am distributing a PowerShell script to my team. The script is to fetch an IP address from the Vsphere client, make an mstsc connection, and log it in a shared file.

The moment they used the script they got to know the IP address of machine. After that, they always tend to use mstsc directly instead of running the PowerShell script. (As they are using mstsc I am not able to know whether they are using the VM frequently or not.)

Mainly they are telling me that running PowerShell is not straightforward.

I am sick by their laziness.

Is there a way to make a PowerShell script work by double clicking a .ps1 file?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Samselvaprabu
  • 16,830
  • 32
  • 144
  • 230
  • 3
    How about using logs on the server? "Don't trust the user" may mean you can't trust him with your data, but more often it means don't trust him to not be lazy. –  Apr 13 '12 at 07:56
  • See also http://www.howtogeek.com/204166/how-to-configure-windows-to-work-with-powershell-scripts-more-easily/ – Michael Freidgeim Feb 19 '16 at 03:05
  • Compiling a PS script by using Powershell studio https://www.sapien.com/software/powershell_studio – Root Loop Nov 09 '18 at 16:53
  • I prefer to run it through vscode (so I can see the consol, and debug it) – JinSnow May 30 '19 at 07:15
  • 5
    I am also sick by the user's laziness, plus one.. – Timo Aug 17 '20 at 14:33

22 Answers22

198

Create a shortcut with something like this as the "Target":

powershell.exe -command "& 'C:\A path with spaces\MyScript.ps1' -MyArguments blah"
David Brabant
  • 41,623
  • 16
  • 83
  • 111
  • 5
    As Jeffery Hicks(He is an authoritative resource in powershell) advised that it is not recommended to change the settings, I would vote your answer is the best answer and JPBlanc answer as useful answer. – Samselvaprabu Apr 16 '12 at 05:41
  • 9
    use this with: ` -noLogo -ExecutionPolicy unrestricted -file ` as answered by: @user3071883 – Ujjwal Singh Jan 03 '16 at 14:30
  • 1
    @UjjwalSingh: `-NoProfile` is also helpful, since the profile can do all kinds of things that the script doesn't expect. – Joey Apr 03 '17 at 06:28
  • 7
    For future readers: There is a registry hack in the answers below that accomplishes what OP asked for. The accepted answer does not. – Wouter Apr 14 '17 at 09:47
  • This does not work for some of my .ps1 scripts, however the .bat solution by @rick works like a charm - if I use the absolute path the .bat works as a shortcut to double click. `start powershell -command "& '.\MyPowershellScript.ps1' -MyArguments blah"` – RBA Jan 04 '18 at 07:45
  • I found tweaking this answer slightly by adding execution policy helped: `@echo off powershell.exe -ExecutionPolicy ByPass -command "& 'C:\A path with spaces\MyScript.ps1' -MyArguments blah"` – Sashah May 25 '18 at 17:46
  • 2
    Is there a way to do this but using a relative / the current path? – mythofechelon Aug 15 '18 at 14:37
  • I got it to work using `powershell.exe -noLogo -ExecutionPolicy unrestricted -file "C:\Users\Some Path\script.ps1"` –  Jul 04 '19 at 10:02
  • If the PS1 file name contains special characters: Add the line [`CHCP 65001`](https://superuser.com/a/869630/) to the beginning of the BAT file. Save the BAT file in UTF-8 format. – root Nov 11 '19 at 15:09
  • PS Ise freezes with `powershell.exe -command "& '$PSScriptRoot/arbeitstage.ps1'"`. This did not work either: `powershell.exe -noLogo -ExecutionPolicy unrestricted -file "C:\Users\Some Path\script.ps1"`. Relative path does not work. – Timo Aug 17 '20 at 14:44
  • @Wouter Technically the below answer is more true to OP's request, but it is far less safe. Depending on the application/environment OP is working towards/in, this is the more correct answer. That said, I am having issues getting my script to write lines using this method. It only seems to want to work properly when run as a command line in powershell. – pmackni Jul 01 '21 at 14:24
  • I take back the part about my script not working, I accidentally left an uninitialzied powershell parameter in my foreach loop of my script from when I was testing in powershell. Carry on. – pmackni Jul 01 '21 at 15:24
  • An article with similar examples : https://www.tenforums.com/tutorials/97162-powershell-scripting-run-script-shortcut.html – user160357 May 31 '22 at 06:47
115

Or if you want all PS1 files to work the way VBS files do, you can edit the registry key at:

HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\open\command

On Windows 11 the location has changed to:

HKEY_CLASSES_ROOT\Applications\powershell.exe\shell\open\command

Edit the Default value to be something like so...

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -noLogo -ExecutionPolicy unrestricted -file "%1"

Then you can just double click all your .PS1 files like you would like to. in my humble opinion, be able to out of the box.

I'm going to call this "The Powershell De-castration Hack". LOL enjoy!

Sean Bannister
  • 3,105
  • 4
  • 31
  • 43
user3071883
  • 1,183
  • 1
  • 7
  • 2
  • 59
    This is the only one that worked for me in this list of answers. I'm not prepared to accept "It's not advised" as an answer - I tell the PC what to do, the PC does not tell me. – Matt Nov 13 '14 at 01:19
  • 14
    This is actually a double-bypass. Removes the restriction against double-click-to-run, *and* gets around PowerShell's ExecutionPolicy which is meant to prevent running of untrusted scripts even from the console. – Iszi Nov 26 '14 at 13:20
  • 6
    This is the wrong way to do it. You're changing what the "Open" command does instead of setting the "Run with PowerShell" as the default. See itgala.xyz answer below. – Indy411 Oct 10 '16 at 04:52
  • 7
    If you do want do use this method, the correct default value for the registry key is actually`"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -File "%1"%*` where you can optionally include the `-ExecutionPolicy unrestricted`. Note the change at the end of the command which allows zero or more parameters to be passed to the PowerShell script. Quotation marks and spacing around the percent signs should be exactly as shown. – Glenn Slayden Nov 16 '16 at 22:39
  • 1
    This alone did not work for me on Win 7 64bit. Only the combination of this answer and @Paul Fijma's answer worked. Used the command line from this answer, and applied the commands from his. – ajeh Jul 05 '17 at 15:24
  • 5
    For a command-line solution rather than editing the registry, you can type `ftype Microsoft.PowerShellScript.1=^"^%SystemRoot^%\System32\WindowsPowerShell\v1.0\powershell.exe^" -NoLogo -ExecutionPolicy Unrestricted -File ^"%1^" %*` which will accomplish the same. – Damian T. Nov 27 '18 at 16:32
  • Probably you would use `cmd /c ftype` as [here](https://stackoverflow.com/questions/33571900/assoc-and-ftype-do-not-work-under-powershell) to improve @DamianT.answer. – Timo Nov 02 '20 at 10:57
  • @DamianT it is also possible to use `set-itemproperty` instead of the `cmd` hack. – Timo Nov 02 '20 at 11:01
  • I like to add `-noexit` – Hannes Schneidermayer Oct 24 '21 at 21:59
  • This is a great alternative to the accepted answer. Either you set it per file or system-wide. – Tyler Montney Dec 15 '21 at 15:37
61

This worked for me on Windows 10 and powershell 5.1:

  • right click on the .ps1 file
  • Open with...
  • Choose another app
  • Copy the location of powershell.exe to the address bar (by default it won't show windows folder) i.e. C:\Windows\System32\WindowsPowerShell\v1.0
  • select powershell.exe
  • select "Always use this app to open .ps1 files"
  • click OK
vizmi
  • 1,774
  • 17
  • 15
  • 5
    Probably you need to set execution policy on the host machine to something reasonable i.e. RemoteSigned – vizmi Aug 23 '17 at 05:09
  • 6
    This is several years old and nobody has thought of this until half a year ago? -- I did this as well and it works perfectly, though you have to do some more configuration or powershell will refuse to run any .ps1 scripts at all (see `Set-ExecutionPolicy`). – DarkWiiPlayer Feb 02 '18 at 08:25
  • 2
    Seems like the script name needs to contain no spaces if you want this to work. Other than that it appears to work perfectly. – Luke Sep 07 '18 at 12:57
  • 6
    How is this not the highest-voted answer? Why would we edit the registry directly or use batch files when you can just do it the way Windows intended, via file associations? – Dr. Kickass Jan 16 '20 at 21:14
  • @Dr.Kickass It is probably one of the newer answers I guess – vizmi Jan 17 '20 at 04:43
  • That's a really easy solution, worked like a charm. – Honduriel Sep 01 '20 at 08:35
  • 5
    Excellent answer. If you need to set the execution policy -- this worked for me `Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned` – Josh May 26 '21 at 19:50
  • Excellent answer, by far the safest way of doing it. – Ryan Mckenna Dec 11 '22 at 14:30
22

I agree that setting a system setting may be a bit much, but the shortcut requiring a hardcoded path is not ideal. A bat file actually solves the problem nicely

RunMyPowershellScript.bat

 start powershell -command "& '.\MyPowershellScript.ps1' -MyArguments blah"

This batch file can now be double clicked on, shortcuts can be easily created to the batch file, and the script can be deployed to any folder.

Rick
  • 426
  • 3
  • 8
  • Note you need the ps1 script in same folder as the bat file. You gain copy paste ability at the cost of maintaining 2 files. – jiggunjer Aug 16 '16 at 06:59
  • @rick, Did not work for me, nothing happens except a black window popping up for some milliseconds: `start powershell -command "& 'C:\Users\skripte\arbeitstage.ps1'"` – Timo Aug 17 '20 at 14:53
  • 1
    @Timo Try running your command in a regular command prompt window so you can see what the output is before the window disappears. – Rick Aug 18 '20 at 16:17
  • 1
    @Timo use -file instead of -command, also remove the & – Muizz Mahdy Dec 14 '21 at 14:28
21

Be aware that one of PowerShell's security features is that users can NOT launch script with a double click. Use great care if you modify this setting. An alternative might be to package your script. Some editors like PrimalScript can do that. The users still need PowerShell installed but then they can double-click the exe. And it sounds like your team needs a little education.

Jeffery Hicks
  • 947
  • 5
  • 8
  • 4
    It's a security feature because it delays hackers by *at least* a few minutes until they find an alternative! – MarcH Jan 24 '16 at 06:19
  • 70
    It's a stupid and useless security feature. Can you run a batch by double ckicling on it? Yes, An exe? Yes. A VBScript? Yes. I fail to realize why a poweshell script would be considered more dangrous than all the others – Mauro F. Aug 27 '17 at 05:53
  • 13
    I don't know about you guys, but I've definitely ran a bat file on the wrong computer before with an accidental double-click. With something meant to automate administration, it makes perfect sense to restrict that. – lordcheeto Sep 19 '17 at 16:44
  • 2
    You can double-click an EXE file so I don't see how this helps. But surely the Powershell team is aware of this basic argument. There must be more to it. – usr Apr 04 '19 at 12:14
  • 7
    If you write scripts that are "dangerous" to execute, and you don't add a basic confirmation process... I reckon you like to live dangerously. – Romain Vincent May 23 '20 at 14:25
18

I wrote this a few years ago (run it with administrator rights):

<#
.SYNOPSIS
    Change the registry key in order that double-clicking on a file with .PS1 extension
    start its execution with PowerShell.
.DESCRIPTION
    This operation bring (partly) .PS1 files to the level of .VBS as far as execution
    through Explorer.exe is concern.
    This operation is not advised by Microsoft.
.NOTES
    File Name   : ModifyExplorer.ps1
    Author      : J.P. Blanc - jean-paul_blanc@silogix-fr.com
    Prerequisite: PowerShell V2 on Vista and later versions.
    Copyright 2010 - Jean Paul Blanc/Silogix
.LINK
    Script posted on:
    http://www.silogix.fr
.EXAMPLE
    PS C:\silogix> Set-PowAsDefault -On
    Call Powershell for .PS1 files.
    Done!
.EXAMPLE
    PS C:\silogix> Set-PowAsDefault
    Tries to go back
    Done!
#>
function Set-PowAsDefault
{
  [CmdletBinding()]
  Param
  (
    [Parameter(mandatory=$false, ValueFromPipeline=$false)]
    [Alias("Active")]
    [switch]
    [bool]$On
  )

  begin
  {
    if ($On.IsPresent)
    {
      Write-Host "Call PowerShell for .PS1 files."
    }
    else
    {
      Write-Host "Try to go back."
    }
  }

  Process
  {
    # Text Menu
    [string]$TexteMenu = "Go inside PowerShell"

    # Text of the program to create
    [string] $TexteCommande = "%systemroot%\system32\WindowsPowerShell\v1.0\powershell.exe -Command ""&'%1'"""

    # Key to create
    [String] $clefAModifier = "HKLM:\SOFTWARE\Classes\Microsoft.PowerShellScript.1\Shell\Open\Command"

    try
    {
      $oldCmdKey = $null
      $oldCmdKey = Get-Item $clefAModifier -ErrorAction SilentlyContinue
      $oldCmdValue = $oldCmdKey.getvalue("")

      if ($oldCmdValue -ne $null)
      {
        if ($On.IsPresent)
        {
          $slxOldValue = $null
          $slxOldValue = Get-ItemProperty $clefAModifier -Name "slxOldValue" -ErrorAction SilentlyContinue
          if ($slxOldValue -eq $null)
          {
            New-ItemProperty $clefAModifier -Name "slxOldValue" -Value $oldCmdValue  -PropertyType "String" | Out-Null
            New-ItemProperty $clefAModifier -Name "(default)" -Value $TexteCommande  -PropertyType "ExpandString" | Out-Null
            Write-Host "Done !"
          }
          else
          {
            Write-Host "Already done!"
          }
        }
        else
        {
          $slxOldValue = $null
          $slxOldValue = Get-ItemProperty $clefAModifier -Name "slxOldValue" -ErrorAction SilentlyContinue
          if ($slxOldValue -ne $null)
          {
            New-ItemProperty $clefAModifier -Name "(default)" -Value $slxOldValue."slxOldValue"  -PropertyType "String" | Out-Null
            Remove-ItemProperty $clefAModifier -Name "slxOldValue"
            Write-Host "Done!"
          }
          else
          {
            Write-Host "No former value!"
          }
        }
      }
    }
    catch
    {
      $_.exception.message
    }
  }
  end {}
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JPBlanc
  • 70,406
  • 17
  • 130
  • 175
  • 2
    I've had to change it at this location for it to work: Computer\HKEY_CLASSES_ROOT\Applications\powershell.exe\Shell\Open\Command. I don't understand the registry enough to explain why this is. Maybe because i'm running Powershell 3.0? Anyway, thanks for the script. – Wouter Dec 12 '12 at 11:44
17

You'll need to tweak registry. First, configure a PSDrive for HKEY_CLASSES_ROOT since this isn’t set up by default. The command for this is:

New-PSDrive HKCR Registry HKEY_CLASSES_ROOT

Now you can navigate and edit registry keys and values in HKEY_CLASSES_ROOT just like you would in the regular HKCU and HKLM PSDrives.

To configure double-clicking to launch PowerShell scripts directly:

Set-ItemProperty HKCR:\Microsoft.PowerShellScript.1\Shell '(Default)' 0

To configure double-clicking to open PowerShell scripts in the PowerShell ISE:

Set-ItemProperty HKCR:\Microsoft.PowerShellScript.1\Shell '(Default)' 'Edit'

To restore the default value (sets double-click to open PowerShell scripts in Notepad):

Set-ItemProperty HKCR:\Microsoft.PowerShellScript.1\Shell '(Default)' 'Open'
Alex Langer
  • 456
  • 4
  • 10
  • I tried restoring the default value using the last line from an Elevated PowerShell ISE, but apparently if you have used a command like Thermionix described it replaces the Default, so you have to reset the Shell.command explicitly (but I haven't found the correct syntax for that so I had to go through the Control Panel> Programs> File Associations). – dragon788 Sep 13 '18 at 16:39
13

Simple PowerShell commands to set this in the registry;

New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
Set-ItemProperty -Path "HKCR:\Microsoft.PowerShellScript.1\Shell\open\command" -name '(Default)' -Value '"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -noLogo -ExecutionPolicy unrestricted -file "%1"'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Thermionix
  • 669
  • 1
  • 6
  • 17
  • 6
    For a bit more security I would suggest using `ExecutionPolicy RemoteSigned` so that a random script downloaded from the network/internet (with the metadata denoting it wasn't created locally) won't execute from a double click, but any file you have created by copy and pasting code into a .ps1 file will execute because it doesn't have that metadata associated with it. – dragon788 Sep 13 '18 at 16:18
  • It worked in Windows 10. Now in Windows 11 it says: Set-ItemProperty: Cannot find path 'HKCR:\Microsoft.PowerShellScript.1\Shell\open\command' because it does not exist. – hyperion385 Oct 28 '21 at 08:41
  • doesn't work in Windows 11 – Alper Ebicoglu Dec 29 '21 at 11:36
11

You may set the default file association of ps1 files to be powershell.exe which will allow you to execute a powershell script by double clicking on it.

In Windows 10,

  1. Right click on a ps1 file
  2. Click Open with
  3. Click Choose another app
  4. In the popup window, select More apps
  5. Scroll to the bottom and select Look for another app on this PC.
  6. Browse to and select C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.
  7. List item

That will change the file association and ps1 files will execute by double-clicking them. You may change it back to its default behavior by setting notepad.exe to the default app.

Source

datu-puti
  • 1,306
  • 14
  • 33
11

I tried the top-most answers to this question, but encountered error messages. Then I found the answer here:

PowerShell says "execution of scripts is disabled on this system."

What worked well for me was to use this solution:

powershell -ExecutionPolicy Bypass -File script.ps1

You can paste that into a .bat file and double-click on it.

Francis
  • 702
  • 9
  • 21
11

A solution in the same spirit as UNIX shar (shell archive).

You can put your powershell script in a file with the .cmd extension (instead of .ps1), and put this at the start:

@echo off
Rem Make powershell read this file, skip a number of lines, and execute it.
Rem This works around .ps1 bad file association as non executables.
PowerShell -Command "Get-Content '%~dpnx0' | Select-Object -Skip 5 | Out-String | Invoke-Expression"
goto :eof
# Start of PowerShell script here
Johan Boulé
  • 1,936
  • 15
  • 19
  • 1
    This is a great solution. My attempt was to convert the .ps1 file to base64 with: certutil -f -encode and append the contents to the .bat file and have the first lines (semicolons should be newlines): `@echo off; set ps1file=%TEMP%\myprogram.ps1; certutil -f -decode %0 %ps1file% > nul; powershell.exe -ExecutionPolicy Bypass -File %ps1file%; exit` – Kjeld Mar 05 '23 at 02:34
  • i like this solution in general but it doesnt work with network pathes – Floyd Apr 18 '23 at 09:55
  • 1
    i can solve this problem with this change: `PowerShell -Command "$s = Get-Content '%~dpnx0' | Select-Object -Skip 5 | Out-String; $s='Set-Location ""%~pd0"";'+$s; Invoke-Expression $s"` – Floyd Apr 18 '23 at 10:42
8

put a simple .cmd file in my subfolder with my .ps1 file with the same name, so, for example, a script named "foobar" would have "foobar.ps1" and "foobar.cmd". So to run the .ps1, all I have to do is click the .cmd file from explorer or run the .cmd from a command prompt. I use the same base name because the .cmd file will automatically look for the .ps1 using its own name.

::====================================================================
:: Powershell script launcher
::=====================================================================
:MAIN
    @echo off
    for /f "tokens=*" %%p in ("%~p0") do set SCRIPT_PATH=%%p
    pushd "%SCRIPT_PATH%"

    powershell.exe -sta -c "& {.\%~n0.ps1 %*}"

    popd
    set SCRIPT_PATH=
    pause

The pushd/popd allows you to launch the .cmd file from a command prompt without having to change to the specific directory where the scripts are located. It will change to the script directory then when complete go back to the original directory.

You can also take the pause off if you want the command window to disappear when the script finishes.

If my .ps1 script has parameters, I prompt for them with GUI prompts using .NET Forms, but also make the scripts flexible enough to accept parameters if I want to pass them instead. This way I can just double-click it from Explorer and not have to know the details of the parameters since it will ask me for what I need, with list boxes or other forms.

KoZm0kNoT
  • 462
  • 6
  • 9
  • 3
    Thanks for the batch file. Just so you know, you can replace the "for" statement with just: "set SCRIPT_PATH=%~p0" (without quotes). In fact, I would use ~dp0 instead of ~p0 so it will work across drives. I'll use this myself. – zumalifeguard Jun 12 '15 at 18:49
8
  1. Navigate REGEDIT to HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Microsoft.PowerShellScript.1\Shell
  2. On the right pane, double-click "(Default)"
  3. Delete existing value of "Open" (which launches Notepad) and type "0" (being zero, which launches Powershell directly).

Revert the value if you wish to use Notepad as the default again.

gth
  • 81
  • 1
  • 1
4
  • there is my solution 2022

  • Install "PowerShell-7.2.2-win-x64.msi"

  • Right click on file.ps1 and change to exec with "pwsh"

Powershell registry hacks and policy bypass never worked for me. enter image description here

DIMM_V2
  • 105
  • 1
  • 9
3

If you are familiar with advanced Windows administration, then you can use this ADM package (instructions are included on that page) and allow running PowerShell scripts after double click via this template and Local GPO. After this you can simply change default program associated to .ps1 filetype to powershell.exe (use search, it's quite stashed) and you're ready to run PowerShell scripts with double click.

Otherwise, I would recommend to stick with other suggestions as you can mess up the whole system with these administrations tools.

I think that the default settings are too strict. If someone manages to put some malicious code on your computer then he/she is also able to bypass this restriction (wrap it into .cmd file or .exe, or trick with shortcut) and all that it in the end accomplishes is just to prevent you from easy way of running the script you've written.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jirrick
  • 31
  • 2
  • Once installed you can find this setting in the "Local Group Policy Editor" under: Local Computer Policy -> Computer Configuration -> Administrative Templates -> Windows Components. There, you can enable it, and make sure to select a policy in the drop-down list that becomes enabled then. – Wouter Dec 12 '12 at 11:06
2

I used this (need to run it only once); also make sure you have rights to execute:

from PowerShell with elevated rights:

Set-ExecutionPolicy=RemoteSigned

then from a bat file:

-----------------------------------------

 ftype Microsoft.PowerShellScript.1="C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe" -noexit ^&'%%1'

 assoc .ps1=Microsoft.PowerShellScript.1

-----------------------------------------
auto exit: remove -noexit 

and voila; double-clicking a *.ps1 will execute it.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Paul Fijma
  • 467
  • 4
  • 9
  • `The term '%1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.` – ajeh Jul 05 '17 at 15:18
  • Only the combination of the accepted answer with this answer worked for me, once I used the command from the accepted answer in the registry key. Apparently running `assoc` is required. – ajeh Jul 05 '17 at 15:25
  • @ajeh (sorry for bieng leet to the party. If you get that error, its because you have thw wrong single quotes. make sure you use UTF-8. (use notepad++; drop the two strings, save as a runme.cmd file) – Paul Fijma Jul 22 '19 at 06:27
  • For the batch file part, I executed the first two lines `ftype` and `assoc` directly on the cmd line with admin privs. Had to remove one % to get it to work. – Adrian Sep 28 '21 at 19:19
2

This is based on KoZm0kNoT's answer. I modified it to work across drives.

@echo off
pushd "%~d0"
pushd "%~dp0"
powershell.exe -sta -c "& {.\%~n0.ps1 %*}"
popd
popd

The two pushd/popds are necessary in case the user's cwd is on a different drive. Without the outer set, the cwd on the drive with the script will get lost.

zumalifeguard
  • 8,648
  • 5
  • 43
  • 56
2

This is what I use to have scrips run as admin by default:

Powershell.exe -Command "& {Start-Process PowerShell.exe -Verb RunAs -ArgumentList '-File """%1"""'}"

You'll need to paste that into regedit as the default value for:

HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Open\Command

Or here's a script that will do it for you:

$hive = [Microsoft.Win32.RegistryKey]::OpenBaseKey('ClassesRoot', 'Default')
$key = $hive.CreateSubKey('Microsoft.PowerShellScript.1\Shell\Open\Command')
$key.SetValue($null, 'Powershell.exe -Command "& {Start-Process PowerShell.exe -Verb RunAs -ArgumentList ''-File """%1"""''}"')
Mica
  • 383
  • 3
  • 14
2

In Windows 10 you might also want to delete Windows Explorer's override for file extension association:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.ps1\UserChoice

in addition to the HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\open\command change mentioned in other answers.

See https://stackoverflow.com/a/2697804/1360907

Chris Xue
  • 2,317
  • 1
  • 25
  • 21
2

You may not want to but an easy way is just to create a .BAT file and put your command in:

powershell ./generate-strings-table-en.ps1
powershell ./generate-conjoined-tables-it.ps1

Then double-click said BAT file.

Epirocks
  • 480
  • 4
  • 11
1

You can use the Windows 'SendTo' functionality to make running PS1 scripts easier. Using this method you can right click on a PS1 script and execute. This is doesn't exactly answer the OP question but it is close. Hopefully, this is useful to others. BTW.. this is helpful for a variety of other tasks.

  • Locate / Search for Powershell.exe
  • Right click on Powershell.exe and choose Open File Location
  • Right click on Powershell.exe and choose Create Shortcut. Temporarily save some place like your desktop
  • You might want to open as Admin by default. Select Shortcut > Properties > Advanced > Open As Admin
  • Open the Sendto folder. Start > Run > Shell:Sendto
  • Move the Powershell.exe shortcut to the Sendto folder
  • You should now be able to right click on a PS1 script.
  • Right Click on a PS1 file, Select the SendTo context option > Select the Powershell shortcut
  • Your PS1 script should execute.
MrRobot
  • 19
  • 1
0

From http://www.howtogeek.com/204166/how-to-configure-windows-to-work-with-powershell-scripts-more-easily:

Set the default value for the HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell to 0

mstone
  • 414
  • 4
  • 11