Wednesday, May 10, 2017

windows - How to launch PowerShell script from the OS command line?



I have a PowerShell script for building my project files, and I'd like to have capability to run it from my file manager's command line (and, possibly, make a shortcut to this script so I can start build from my desktop)
Any way to do this?


Answer




If you want to make powershell scripts behave a little more like typical cmd\bat scripts where you can just type the name and have them execute you can set up the association and file type to tell the shell to pass the correct parameters to Powershell and set the patheext variable so you just have to type the name of the script rather than the full name.ext.



First check that the .ps1 extension is associated with Powershell - it should be but it's worth making sure:



assoc .ps1


This should give you something like the following:



.ps1=Microsoft.PowerShellScript.1



Now override the default open behaviour (open with notepad) for this file type with the Powershell commandline syntax for calling a script. We need to add additional quotes around the parameter &1 to handle paths with spaces.



ftype Microsoft.PowerShellScript.1="c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe" "& '&1'"


If you really want Powershell scripts to act like other script files then modify the pathext environment variable. Ideally set this in via My Computer->Properties->Advanced System Settings->Advanced tab->Environment Variables but you can temporarily set this in a cmd shell via:



set pathext=.ps1;%pathext%


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...