Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way of writing a batch-script that's executed via C:\>cmd.exe a.bat to switch into powershell.exe and run the remaining lines of code?

1. | echo "Hi. I'm cmd.exe."
2. | powershell
3. | echo "Now, I'm PowerShell.exe! Look:"
4. | get-random

This just stops after line #2 waiting for input.

share|improve this question
See this StackOverflow question: How to execute powershell commands from a batch file? – Siim K Feb 20 '12 at 18:53
So, looks like one just simply puts: powershell -Command "& {echo "Now, I''m PowerShell.exe! Look:";get-random}" Thanks! – Firebowl2000 Feb 20 '12 at 19:54

You can write a .bat file and use this for the first line:

;@Findstr -bv ;@F "%~f0" | powershell -noprofile -command - & goto:eof

This line pipes all lines from the current file ("%~f0") that don't start with ";@F" to PowerShell to be executed. The remaining lines would be PowerShell code. Here is an example.

;@Findstr -bv ;@F "%~f0" | powershell -noprofile -command - & goto:eof

$procCount = (Get-Process).Count
Write-Host "There are $procCount processes running right now." -fore green
Start-Sleep 5
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .