PoshCode Logo PowerShell Code Repository

ExecutePowerShell.cmd by Joel Bennett 6 years ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2447"></script>download | new post

This is a batch file … with a PowerShell script inside. It’s my answer to all those “compile your .ps1” solutions that are floating around. Why would you do that, when PowerShell still has to be installed?

In any case, just replace the last two lines of the file with … whatever script you want to execute, and hand it out as a .cmd batch file. People can double-click it and it should “just work”

Note that it runs with ExecutionPolicy unrestricted and -NoProfile to make sure the environment is the same on everyone’s PC…

  1. :: <#
  2. copy %0 %0.ps1
  3. PowerShell.exe -ExecutionPolicy Unrestricted -NoProfile -Command "$ErrorActionPreference = 'SilentlyContinue'; . %0.ps1; Remove-Item %0.ps1"
  4. exit
  5. :: #>
  6. $ErrorActionPreference = 'Continue'
  8. ### Your PowerShell script goes below here.
  9. ### I've put a couple of lines as an example ...
  10. ls | sort length -desc | select -first 5 | ft
  11. ps | sort ws -desc | select -first 10 | ft

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

Remember me