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.


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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