Is there a way to get the PowerShell script to work by double-clicking the .ps1 file?

I am distributing PowerShell script for my team. The script is designed to obtain an IP address from a Vsphere client, establish an mstsc connection, and register it in a shared file.

The moment they used the script, they recognized the IP address of the computer. After that, they always tend to use mstsc directly instead of running a PowerShell script. (Since they use mstsc, I cannot know if they often use the virtual machine or not.)

Basically they tell me that starting PowerShell is not easy.

I'm sick of their laziness.

Is there a way to get the PowerShell script to work by double-clicking the .ps1 file?

+103
powershell
Apr 13 2018-12-12T00: 00Z
source share
18 answers

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

powershell.exe -command "& 'C:\A path with spaces\MyScript.ps1' -MyArguments blah" 
+101
Apr 13 2018-12-12T00:
source share

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

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

Change the default value so that it is ...

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

Then you can simply double-click all of your .PS1 files as you would like. in my humble opinion, be in the know.

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

+66
Dec 17 '13 at 0:15
source share

Remember that one of the PowerShell security features is that users cannot run the script with a double click. Be careful if you change this setting. An alternative could be wrapping your script. Some editors, such as PrimalScript, can do this. Users still need to install PowerShell, but then they can double-click the EXE. And it sounds like your team needs a little education.

+18
Apr 13 2018-12-12T00:
source share

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

 <# .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 : JP 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 {} } 
+15
Apr 13 '12 at 8:22
source share

I agree that installing system settings may be a bit, but a shortcut that requires a hard path is not ideal. Bat file really solves the problem beautifully

RunMyPowershellScript.bat

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

Now this batch file can be double-clicked, shortcuts can be easily created in a batch file, and the script can be deployed to any folder.

+13
Aug 12 '14 at 20:08
source share

You will need to configure the registry. First configure PSDrive for HKEY_CLASSES_ROOT, as this is not set by default. The command for this:

 New-PSDrive HKCR Registry HKEY_CLASSES_ROOT 

Now you can navigate and edit registry keys and values ​​in HKEY_CLASSES_ROOT in the same way as in regular HKCU and HKLM PSDrives.

To configure double-click to run PowerShell scripts directly:

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

To configure double-click to open PowerShell scripts in PowerShell ISE:

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

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

 Set-ItemProperty HKCR:\Microsoft.PowerShellScript.1\Shell '(Default)' 'Open' 
+12
Feb 19 '16 at 12:23
source share

This worked for me on Windows 10 and powershell 5.1:

  • right click on the .ps1 file
  • To open with...
  • Select another application.
  • Copy the location of powershell.exe to the address bar (by default it will not display the Windows folder), i.e. C: \ Windows \ System32 \ WindowsPowerShell \ v1.0
  • select powershell.exe
  • select "Always use this application to open .ps1 files"
  • click OK
+9
Aug 23 '17 at 5:06 on
source share

Simple PowerShell commands to install 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"' 
+8
Jun 16 '14 at 7:56
source share
  • Go to REGEDIT at HKEY_LOCAL_MACHINE \ SOFTWARE \ Classes \ Microsoft.PowerShellScript.1 \ Shell
  • In the right pane, double-click "(default)"
  • Delete the existing Open value (which launches Notepad) and enter 0 (zero that launches Powershell directly).

Cancel the value if you want to use Notepad again.

+6
Aug 25 '15 at 9:24
source share

put a simple .cmd file in my subfolder with my .ps1 file with the same name, so, for example, a script named "foobar" will have "foobar.ps1" and "foobar.cmd". Therefore, to run .ps1, all I have to do is click the .cmd file from Explorer or run .cmd from the command line. I use the same base name because the .cmd file will automatically search for .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 

Pushd / popd allows you to run a .cmd file from the command line without having to change the specific directory where the scripts are located. It will change to the script directory, then return it to the original directory.

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

If my .ps1 script has parameters, I request them with GUI hints using .NET Forms, but also make the scripts flexible enough to accept parameters if I want to pass them. That way, I can just double-click on it from Explorer and not know the details of the parameters, as it will ask me what I need with lists or other forms.

+3
May 20 '15 at 18:57
source share

If you are familiar with advanced Windows administration, you can use this ADM package (the instructions are on this page) and allow PowerShell scripts to run after double-clicking on this template and the local GPO. After that, you can simply change the default program associated with the .ps1 file type to powershell.exe (use the powershell.exe search, it's pretty hidden), and you are ready to run PowerShell scripts with a double click.

Otherwise, I would recommend sticking with other suggestions, as you can ruin the entire system using these administration tools.

I think the default settings are too strict. If someone manages to place malicious code on your computer, he / she will also be able to circumvent this restriction (wrap it in a .cmd or .exe file, or use a shortcut with a shortcut), and all that he ultimately does is it only prevents you from the easy way to run the script you wrote.

+2
Jun 16 2018-12-12T00:
source share

This is based on the answer of KoZm0kNoT. I changed it to work with disks.

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

Two pushd / popds are needed if the custom cdd is on a different drive. Without external dialing, cwd on the disk with the script will be lost.

+2
Jun 12 '15 at 7:10
source share

This is what I use to run default scripts as admin:

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

You need to insert this into regedit as the default value for:

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

Or here is a script that will do this 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"""''}"') 
+2
Jan 17 '16 at 6:32
source share

You can use the Windows SendTo functionality to simplify the execution of PS1 scripts. Using this method, you can right-click the PS1 script and execute. This does not exactly answer the OP question, but it is close. Hope this is helpful to others. BTW .. it is useful for many other tasks.

  • Find / Search Powershell.exe
  • Right-click on Powershell.exe and select Open File Location
  • Right-click on Powershell.exe and select Create Shortcut. Temporarily save a place, such as a desktop.
  • You can open as admin by default. Select Shortcut> Properties> Advanced> Open as Admin
  • Open the Sendto folder. Start> Run> Shell: Send
  • Move the shortcut to Powershell.exe to the Sendto folder
  • Now you can right-click on the PS1 script.
  • Right-click on the PS1 file, select the SendTo context option> Select the Powershell shortcut
  • Your PS1 script should execute.
+2
Jul 28 '17 at 1:35 on
source share

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

from PowerShell with elevated privileges:

 Set-ExecutionPolicy=RemoteSigned 

then from 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 * .ps1 will launch it.

+1
Jun 04 '15 at 13:21
source share

You can set the default file ps1 for ps1 files as powershell.exe which allows you to execute the powershell script by double-clicking on it.

On windows 10

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

This will change the file association, and ps1 files will be executed by double-clicking on them. You can change it to default behavior by installing notepad.exe in the default application.

Source

0
Jul 17 '18 at 2:30
source share

In Windows 10, you can also remove Windows Explorer overrides to associate file extensions:

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

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

See https://stackoverflow.com>

0
Jun 14 '19 at 14:03
source share

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

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

-one
Nov 26 '15 at 22:04
source share



All Articles