This post is coming after a discussion on twitter about how to submit arguments ton an .exe files after a tips on powershell.com.
I’m not agree at all with this technic, this is not a “powershell” way to do things. All the time i say that there are many ways to script and bypass a problem, and of course this one is a solution but other options exist, and theyr are way far better !
As we had see, it’s possible to call directly the exe. You can also do the following:
& 'E:\temp\your\path\here\foo.exe' '-arg1 -arg2 -argn'
This is also (i admit) a badway to reach our goal, but this is an option…
Another option with an .NET interesting approach.
[System.Diagnostics.Process]::Start('E:\temp\your\path\here\foo.exe', " /wait /run /SilentMode").waitforexit()
This method bring the Waitforexit method, which is very interesting as your script will stop the flow until the third party executable has finished is execution.
Another possibility with Invoke-Expression:
Invoke-Expression 'E:\temp\your\path\here\foo.exe /wait /run /SilentMode'
It’ll execute the program with arguments and won’t release prompt until finished !
And, finally the best method ever to call an executable, PowerShell devs had done for us a perfect cmdlet : Start-process ! This cmdlet, by the way use the System.Diagnostics.Process class 😉
Start-Process -FilePath E:\path\foo.exe -ArgumentList "/exit /run" -Wait -NoNewWindow
You can do for example this, which will execute the foo.exe with args, wait for the command to finish to release the prompt and prevent a new CMD window to be created. There are more options you can use, just don’t be shy and try “Get-Help Start-Process”
All for today.
NB: More explanations about how it works behind the scene: http://huddledmasses.org/the-problem-with-calling-legacy-or-native-apps-from-powershell/
NB2: Another possibilities here: http://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx