Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 572
Samselvaprabu Avatar asked Apr 13 '12 07:04

Samselvaprabu


People also ask

What happens when you double click a PowerShell script file?

What happens when you double-click a PowerShell script file? It opens as a Notepad file.

Can I run a PowerShell script from a batch file?

Use the -File Parameter to Run a PowerShell Script From a Batch File. You can invoke a PowerShell script using the -File parameter. It is the simple command to run a PowerShell script from the command prompt.


4 Answers

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

powershell.exe -command "& 'C:\A path with spaces\MyScript.ps1' -MyArguments blah"
like image 67
David Brabant Avatar answered Oct 07 '22 23:10

David Brabant


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

HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\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!

like image 31
user3071883 Avatar answered Oct 07 '22 22:10

user3071883


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
like image 53
vizmi Avatar answered Oct 07 '22 23:10

vizmi


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.

like image 23
Jeffery Hicks Avatar answered Oct 07 '22 23:10

Jeffery Hicks