How to run an EXE file in PowerShell with parameters with spaces and quotes
How do you run the following command in PowerShell?
C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe -verb:sync -source:dbfullsql="Data Source=mysource;Integrated Security=false;User ID=sa;Pwd=sapass!;Database=mydb;" -dest:dbfullsql="Data Source=.\mydestsource;Integrated Security=false;User ID=sa;Pwd=sapass!;Database=mydb;",computername=10.10.10.10,username=administrator,password=adminpass"
When PowerShell sees a command starting with a string it just evaluates the string, that is, it typically echos it to the screen, for example:
PS> "Hello World" Hello World
If you want PowerShell to interpret the string as a command name then use the call operator (&) like so:
PS> & 'C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe'
After that you probably only need to quote parameter/argument pairs that contain spaces and/or quotation chars. When you invoke an EXE file like this with complex command line arguments it is usually very helpful to have a tool that will show you how PowerShell sends the arguments to the EXE file. The PowerShell Community Extensions has such a tool. It is called echoargs. You just replace the EXE file with echoargs - leaving all the arguments in place, and it will show you how the EXE file will receive the arguments, for example:
PS> echoargs -verb:sync -source:dbfullsql="Data Source=mysource;Integrated Security=false;User ID=sa;Pwd=sapass!;Database=mydb;" -dest:dbfullsql="Data Source=.\mydestsource;Integrated Security=false;User ID=sa;Pwd=sapass!;Database=mydb;",computername=10.10.10.10,username=administrator,password=adminpass Arg 0 is <-verb:sync> Arg 1 is <-source:dbfullsql=Data> Arg 2 is <Source=mysource;Integrated> Arg 3 is <Security=false;User> Arg 4 is <ID=sa;Pwd=sapass!;Database=mydb;> Arg 5 is <-dest:dbfullsql=Data> Arg 6 is <Source=.\mydestsource;Integrated> Arg 7 is <Security=false;User> Arg 8 is <ID=sa;Pwd=sapass!;Database=mydb; computername=10.10.10.10 username=administrator password=adminpass>
Using echoargs you can experiment until you get it right, for example:
PS> echoargs -verb:sync "-source:dbfullsql=Data Source=mysource;Integrated Security=false;User ID=sa;Pwd=sapass!;Database=mydb;" Arg 0 is <-verb:sync> Arg 1 is <-source:dbfullsql=Data Source=mysource;Integrated Security=false;User ID=sa;Pwd=sapass!;Database=mydb;>
It turns out I was trying too hard before to maintain the double quotes around the connection string. Apparently that isn't necessary because even cmd.exe will strip those out.
BTW, hats off to the PowerShell team. They were quite helpful in showing me the specific incantation of single & double quotes to get the desired result - if you needed to keep the internal double quotes in place. :-) They also realize this is an area of pain, but they are driven by the number of folks are affected by a particular issue. If this is an area of pain for you, then please vote up this PowerShell bug submission.
For more information on how PowerShell parses, check out my Effective PowerShell blog series - specifically item 10 - "Understanding PowerShell Parsing Modes"
UPDATE 4/4/2012: This situation gets much easier to handle in PowerShell V3. See this blog post for details.
Start-Process, If you're writing a Cmd.exe shell script (i.e., a batch file) that runs an If the parameter doesn't contain spaces, the quotes are optional. Run powershell script with parameters from command line (source: on YouTube) Run powershell script with parameters from command line
Just add the & operator before the .exe name. Here is a command to install SQL Server Express in silence mode:
$fileExe = "T:\SQLEXPRADV_x64_ENU.exe" $CONFIGURATIONFILE = "T:\ConfSetupSql2008Express.ini" & $fileExe /CONFIGURATIONFILE=$CONFIGURATIONFILE
Running Executables in PowerShell, This just writes out what the script received as the parameter. From a cmd prompt, execute the script as follows: powershell.exe C:\Scripts\ParamCheck.ps1 -Something "one two" an archive (possibly to a path including spaces) containing several resources including a batch file and PowerShell script. Run powershell script with parameters (source: on YouTube) Run powershell script with parameters
I had spaces in both command and parameters, and this is what worked for me:
$Command = "E:\X64\Xendesktop Setup\XenDesktopServerSetup.exe" $Parms = "/COMPONENTS CONTROLLER,DESKTOPSTUDIO,DESKTOPDIRECTOR,LICENSESERVER,STOREFRONT /PASSIVE /NOREBOOT /CONFIGURE_FIREWALL /NOSQL" $Prms = $Parms.Split(" ") & "$Command" $Prms
It's basically the same as Akira's answer, but this works if you dynamically build your command parameters and put them in a variable.
PowerShell and double quotes on the command line, I'm trying to run an InstallShield setup with a series of parameters from a but worst case scenario, move it to the same folder as the setup.exe? Also, you can use double quotes inside the argumentlist if needed because it's I'm using CScript to run a VBScript file, and I need to pass a command line to the script where the parameter includes both spaces and quotes. The entire command needs to be passed as one parameter to the script. For example: C:\> CScript myscript.vbs //Nologo "cmd.exe /c "dir && del /q *.txt""
This worked for me:
& 'D:\Server\PSTools\PsExec.exe' @('\\184.108.40.206', '-accepteula', '-d', '-i', $id, '-h', '-u', 'domain\user', '-p', 'password', '-w', 'C:\path\to\the\app', 'java', '-jar', 'app.jar')
Just put paths or connection strings in one array item and split the other things in one array item each.
There are a lot of other options here: https://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx
Microsoft should make this way simpler and compatible with command prompt syntax.
Scripting : PowerShell, PowerShell execute command (.exe) with arguments safely (e.g. with spaces). - README.md. So there are several ways to run .exe files with arguments in powershell. Sometimes, one must find a workaround to make it work properly, which can require some further effort and pain :) depending on the way the .exe has been compiled or made bi its creators.
Summary using vshadow as the external executable:
$exe = "H:\backup\scripts\vshadow.exe" &$exe -p -script=H:\backup\scripts\vss.cmd E: M: P:
PowerShell execute command (.exe) with arguments safely (e.g. , PS> & 'C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe' sign ($), or curly braces, you have to make some changes, probably adding some quotes. but was still unable to run msiexec.exe with parameters that contained spaces. I assume you don't want literal quotes passed to the exe, just for -package:<some string here> to be a single argument regardless of spaces in <some string here>. If you want literal quotes to be passed to the exe, use the above code with.
Invoking files with spaces in PowerShell, To invoke an executable (or any file, for that matter) that has spaces in it, you need to use quotes and then use the ampersand (&) operator. If you wanted to pass parameters, you would do this: Running a Command line script from within powershell · invoke-expression with .exe that has spaces in its I'm having an issue with powershell when invoking an exe at a path containing spaces. The term 'C:\Windows' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. It seems to be splitting on the space between
Pass quoted argument string to Start-Process in PowerShell, Pass quoted argument string to Start-Process in PowerShell · powershell. I'm trying to very simply run an executable and pass a file path in as the first argument. In Likewise, if we’re in the target directory already, the “dot slash” (./) notation explicitly instructs PowerShell to treat the file as executable. That’s fine. The problem in the above example is that PowerShell has no earthly idea that subl.exe is an executable. As far as the PowerShell parser is concerned,
Call operator - Run - PowerShell, Also if the command (or the path) contains a space then this will fail. Surrounding a command with quotes will make PowerShell treat it as a string, so in addition An executable filename (.exe), script or function. arguments The call operator will only filenames: & "C:\batch\someutil.exe" test 123 "long path to\some file.txt" If you prefer using here-strings, you can change the above to powershell -file and run your powershell script file. Use here-strings as much as you want there. Option 2. If you prefer not to deal with your special quote character inside calling/processing application/script, you can switch to using single quotes instead.