<p>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:</p>

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

<p>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.</p>

[{'answer_id': 29649, 'author': 'Matt Hamilton', 'author_id': 615, 'author_profile': 'https://Stackoverflow.com/users/615', 'pm_score': 4, 'selected': True, 'text': '<p>Save your script as a .ps1 file and launch it using powershell.exe, like this:</p>\n\n<pre><code>powershell.exe .\\foo.ps1\n</code></pre>\n\n<p>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.</p>\n'}, {'answer_id': 29675, 'author': 'Yaakov Ellis', 'author_id': 51, 'author_profile': 'https://Stackoverflow.com/users/51', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://www.microsoft.com/technet/scriptcenter/topics/winpsh/manual/run.mspx#ECEAC" rel="nofollow noreferrer">Source</a> for Matt\'s answer.</p>\n\n<p>I can get it to run by double-clicking a file by creating a batch file with the following in it:</p>\n\n<pre><code>C:\\WINDOWS\\system32\\windowspowershell\\v1.0\\powershell.exe LocationOfPS1File\n</code></pre>\n'}, {'answer_id': 8597794, 'author': 'Jay Bazuzi', 'author_id': 5314, 'author_profile': 'https://Stackoverflow.com/users/5314', 'pm_score': 4, 'selected': False, 'text': '<p>From <a href="http://blogs.msdn.com/b/jaybaz_ms/archive/2007/04/26/powershell-polyglot.aspx" rel="noreferrer">http://blogs.msdn.com/b/jaybaz_ms/archive/2007/04/26/powershell-polyglot.aspx</a></p>\n\n<p>If you\'re willing to sully your beautiful PowerShell script with a little CMD, you can use a PowerShell-CMD polyglot trick. Save your PowerShell script as a .CMD file, and put this line at the top:</p>\n\n<pre>\n@PowerShell -ExecutionPolicy Bypass -Command Invoke-Expression $(\'$args=@(^&{$args} %*);\'+[String]::Join(\';\',(Get-Content \'%~f0\') -notmatch \'^^@PowerShell.*EOF$\')) & goto :EOF\n</pre>\n\n<p>If you need to support quoted arguments, there\'s a longer version, which also allows comments. (note the unusual CMD commenting trick of double @).</p>\n\n<pre>\n@@:: This prolog allows a PowerShell script to be embedded in a .CMD file.\n@@:: Any non-PowerShell content must be preceeded by "@@"\n@@setlocal\n@@set POWERSHELL_BAT_ARGS=%*\n@@if defined POWERSHELL_BAT_ARGS set POWERSHELL_BAT_ARGS=%POWERSHELL_BAT_ARGS:"=\\"%\n@@PowerShell -ExecutionPolicy Bypass -Command Invoke-Expression $(\'$args=@(^&{$args} %POWERSHELL_BAT_ARGS%);\'+[String]::Join(\';\',$((Get-Content \'%~f0\') -notmatch \'^^@@\'))) & goto :EOF\n</pre>\n'}, {'answer_id': 20060430, 'author': 'deadlydog', 'author_id': 602585, 'author_profile': 'https://Stackoverflow.com/users/602585', 'pm_score': 3, 'selected': False, 'text': '<h2>Run Script Automatically From Another Script (e.g. Batch File)</h2>\n\n<p>As Matt Hamilton suggested, simply create your PowerShell .ps1 script and call it using:</p>\n\n<pre><code>PowerShell C:\\Path\\To\\YourPowerShellScript.ps1\n</code></pre>\n\n<p>or if your batch file\'s working directory is the same directory that the PowerShell script is in, you can use a relative path:</p>\n\n<pre><code>PowerShell .\\YourPowerShellScript.ps1\n</code></pre>\n\n<p>And before this will work you will need to set the PC\'s Execution Policy, which I show how to do down below.</p>\n\n<hr>\n\n<h2>Run Script Manually Method 1</h2>\n\n<p>You can <a href="http://blog.danskingdom.com/allow-others-to-run-your-powershell-scripts-from-a-batch-file-they-will-love-you-for-it/" rel="noreferrer">see my blog post for more information</a>, but essentially create your PowerShell .ps1 script file to do what you want, and then create a .cmd batch file in the same directory and use the following for the file\'s contents:</p>\n\n<pre><code>@ECHO OFF\nSET ThisScriptsDirectory=%~dp0\nSET PowerShellScriptPath=%ThisScriptsDirectory%MyPowerShellScript.ps1\nPowerShell -NoProfile -ExecutionPolicy Bypass -Command "&amp; \'%PowerShellScriptPath%\'"\n</code></pre>\n\n<p>Replacing <strong>MyPowerShellScript.ps1</strong> on the 3rd line with the file name of your PowerShell script.</p>\n\n<p>This will allow you to simply double click the batch file to run your PowerShell script, and will avoid you having to change your PowerShell Execution Policy.</p>\n\n<p>My blog post also shows how to run the PowerShell script as an admin if that is something you need to do.</p>\n\n<hr>\n\n<h2>Run Script Manually Method 2</h2>\n\n<p>Alternatively, if you don\'t want to create a batch file for each of your PowerShell scripts, you can change the default PowerShell script behavior from Edit to Run, allowing you to double-click your .ps1 files to run them.</p>\n\n<p>There is an additional registry setting that you will want to modify so that you can run scripts whose file path contains spaces.  I show how to do both of these things <a href="http://blog.danskingdom.com/fix-problem-where-windows-powershell-cannot-run-script-whose-path-contains-spaces/" rel="noreferrer">on this blog post</a>.</p>\n\n<p>With this method however, you will first need to set your execution policy to allow scripts to be ran.  You only need to do this once per PC and it can be done by running this line in a PowerShell command prompt.</p>\n\n<pre><code>Start-Process PowerShell -ArgumentList \'Set-ExecutionPolicy RemoteSigned -Force\' -Verb RunAs\n</code></pre>\n\n<p><strong>Set-ExecutionPolicy RemoteSigned -Force</strong> is the command that actually changes the execution policy; this sets it to <em>RemoteSigned</em>, so you can change that to something else if you need.  Also, this line will automatically run PowerShell as an admin for you, which is required in order to change the execution policy.</p>\n'}, {'answer_id': 40611486, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>you can use this command : \npowershell.exe -argument c:\\scriptPath\\Script.ps1</p>\n'}]
['https://Stackoverflow.com/questions/29645', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/51/']