vklu4itesvet
vklu4itesvet

Reputation: 385

Run powershell.exe with script body as param and other params

I need to run windows powershell script from my programm. To do that, I just start powershell process with a string arg which contains script body itself:

 Process.Start("powershell.exe", "my script body..");

and it works. But I also need to pass some args to script. It should look like :

Process.Start("powershell.exe", "some args my script body..");

Upvotes: 0

Views: 565

Answers (2)

vklu4itesvet
vklu4itesvet

Reputation: 385

Working solution :

Process.Start("powershell.exe", "-noexit -command &{write-host $args[0]} arg1Value arg2Value");

Upvotes: 0

Loïc MICHEL
Loïc MICHEL

Reputation: 26120

from the doc, you should be able to do something like :

process.Start("powershell.exe","-noprofile -file c:\temp\test.ps1 TEST -noexit")

where TEST is the argument passed to the script

edit after re reading your question :

process.Start("powershell.exe","-noprofile -command {write-host $args[0] } -args 'test'")

Upvotes: 2

Related Questions