How to launch PowerShell script from the OS command line?

10,789

Solution 1

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%

Solution 2

One way to do it is to create a batch file with the appropriate commands.

build.bat

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -command "& 'c:\scripts\build_project.ps1' "

You will have to pass commandline arguments from the batch file to the script if your ps script uses them. You will also have to load all the powershell modules your script uses:

Add-PSSnapin Module-Name
Share:
10,789

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    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?

    • Admin
      Admin almost 14 years
      If said file manager is Far, then you can trivially change what runs when pressing enter on a .ps1 file.
  • Joey
    Joey almost 14 years
    Note that there are good reasons that this is not the default setting.
  • Franklin Yu
    Franklin Yu over 6 years
    @Joey Why? Batch files are legacy, and we should be writing PowerShell scripts anyway…
  • Franklin Yu
    Franklin Yu about 6 years
    @Joey PowerShell has always been encouraged over batch file. Microsoft (as well as many enterprise-level service provider like Oracle) is always very conservative about legacy technologies: AWT is still supported in Java 9, but everyone is using Swing (if not able to jump to JavaFX). I can’t take that as “good reasons”. Having batch file calling PowerShell script is our workaround for Windows bad default, not its excuse for keeping this default. (I would further claim that “workaround” proves that something is wrong.)