views:

1998

answers:

2

I have a few lines of PowerShell code that I would like to use as an automated script. The way I would like it to be able to work is to be able to call it using one of the following options:

  1. One command line that opens PowerShell, executes script and closes PowerShell (this would be used for a global build-routine)
  2. A file that I can double-click to run the above (I would use this method when manually testing components of my build process)

I have been going through PowerShell documentation online, and although I can find lots of scripts, I have been unable to find instructions on how to do what I need. Thanks for the help.

+1  A: 

Save your script as a .ps1 file and launch it using powershell.exe, like this:

powershell.exe .\foo.ps1

Make sure you specify the full path to the script, and make sure you have set your execution policy level to at least "RemoteSigned" so that unsigned local scripts can be run.

Matt Hamilton
A: 

Source for Matt's answer.

I can get it to run by double-clicking a file by creating a batch file with the following in it:

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe LocationOfPS1File
Yaakov Ellis