Executing a PowerShell Script Like a Batch File

powershell_2If you have ever tried to execute a PowerShell script (files ending in .PS1) by double-clicking on the file, you will have noticed that instead of running the script, it opens a text editor. Whatever your reasons, it turns out there is a way to execute PowerShell scripts in this manner. Here’s how.

In short, my method to executing PowerShell scripts in this manner is to put them in a Batch file. When this Batch file runs, it creates a .PS1 script, and then executes it with the “-ExecutionPolicy Bypass” option. You can read more about PS script execution policies on Microsoft’s TechNet site. More information on the standard ways to execute PowerShell scripts is also available here on TechNet. To recap:

  1. Echo each line of the PS1 script to a temporary file.
  2. At the end of the Batch script, execute said file with the command, powershell.exe -ExecutionPolicy Bypass -File <filename>
  3. Delete the temporary script.

To make things easier for myself, I whipped up a bit of a JavaScript converter that will take the pain out of copy/pasting “echo” statements everywhere.

This entry was posted in Javascript, PowerShell. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>