Tom Kustermans
Tom Kustermans

Reputation: 531

running .bat in powershell script without opening any windows

I have written a PowerShell script that is going to interpret a mail's body for command's and create a .bat file to execute the commando's it found. This script works, but the one big issues is that whenever is executes the .bat file, a command-prompt window flashes over the screen real quick. I was wondering if it's possible to prevent this from happening?

My code:

$m.Body | Out-File cmd.bat -Encoding ascii -Append
.\cmd.bat | Out-File results.txt

Is there any command of property i have to set? Thanks.

Upvotes: 0

Views: 6787

Answers (2)

jdawiz
jdawiz

Reputation: 510

I realize this question is more than 2 years old at the time I write this however there is still no official answer. Although I am very new to PowerShell, I think I have a more pure Powershell answer than using vbscript or COM.

Use Invoke Command:

Invoke-Command {cmd.exe /c cmd.bat} | Out-File results.txt

That Should do the trick. This will shell to cmd.exe and the /c will self terminate the shell on completion. It will run within the current shell so no new window will open.

Upvotes: 2

Patrick87
Patrick87

Reputation: 28302

Answers and information can be found here.

From there, the selected answer, in case the link goes stale:

Save the following as wscript, for instance, hidecmd.vbs after replacing "testing.bat" with your batch file's name.

Set oShell = CreateObject ("Wscript.Shell") 
Dim strArgs
strArgs = "cmd /c testing.bat"
oShell.Run strArgs, 0, false

The second parameter of oShell.Run is intWindowStyle value indicating the appearance of the program's window and zero value is for hidden window.

The reference is here http://msdn.microsoft.com/en-us/library/d5fk67ky.aspx

Upvotes: 1

Related Questions