Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.