I want to execute a cmd on PowerShell and this command uses semicolons. Then PowerShell interprets it as multiple commands. How do I make PowerShell ignore the semicolons and execute my command how a unique command?
Example:
Invoke-Expression "msbuild /t:Build;PipelinePreDeployCopyAllFilesToOneFolder /p:Configuration=Debug;_PackageTempDir=$TargetFolder $WebProject"
Another example:
Invoke-Expression "test`;test2"
And the second example response:
The term 'test' 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.
At line:1 char:6
+ teste <<<< ;teste2
+ CategoryInfo : ObjectNotFound: (teste:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
The term 'test2' is not recognized as the name of a cmdlet, function, script file, or operable program. Chec
k the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:13
+ teste;teste2 <<<<
+ CategoryInfo : ObjectNotFound: (teste2:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Best PowerShell Practice and Style: Avoid Using Semicolons ( ; ) as Line Terminators PowerShell will not complain about extra semicolons, but they are unnecessary, and can get in the way when code is being edited or copy-pasted.
Use the -Replace Operator to Escape Special Characters in PowerShell. The -Replace operator replaces texts or characters in PowerShell. You can use it to remove texts or characters from the string. The -Replace operator requires two arguments: the string to find and the string to replace from the given input.
Finally, if your PowerShell strings are quoted with double quotes, then any double quote characters in the string must be escaped with the backtick "`". Alternatively, the embedded double quote characters can be doubled (replace any embedded " characters with "").
To prevent the substitution of a variable value in a double-quoted string, use the backtick character ( ` ), which is the PowerShell escape character.
Just escape the semicolon on the command line:
msbuild /t:Build`;PipelinePreDeployCopyAllFilesToOneFolder /p:Configuration=Debug`;_PackageTempDir=$TargetFolder $WebProject
I do this all the time with the tf.exe utility:
tf.exe status . /r /workspace:WORK`;johndoe
FYI, this issue has been heavily voted up on Connect. PowerShell v3 addresses this issue with the new --%
operator:
$env:TargetFolder = $TargetFolder
msbuild $WebProject --% /t:Build;PipelinePreDeployCopyAllFilesToOneFolder /p:Configuration=Debug;_PackageTempDir=%TargetFolder%
The easiest way to ignore a semicolon? Simply use a single quote versus double quote!
In PowerShell, the type of quote you use matters. A double quote will let PowerShell do string expansion (so if you have a variable $something = someprogram.exe, and run "$something", PowerShell substitutes in "someprogram.exe").
If you don't need string substitution/variable expansion then just use single-quotes. PowerShell will execute single-quoted strings exactly as listed.
Another option if you want to use string expansion is to use a here-string instead. A here string is just like a regular string, however it begins and ends with an @ sign on its own separate line, like so:
$herestring = @"
Do some stuff here, even use a semicolon ;
"@
This is a best-of-both-worlds scenario, as you can use your fancy characters and have them work, but still get Variable expansion, which you do not get with single-quotes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With