Escaping splatted variables in Powershell Invoke-Expression

I'm trying to invoke another application (Beyond Compare) from Powershell which requires an @ in the typical command-line:

C:\deploy>bcompare @static.diff

I've found Powershell's Invoke-Expression, but when I try the following it gives me an error:

PS C:\deploy>Invoke-Expression "bcompare @static.diff"
Invoke-Expression : Cannot expand the splatted variable '@static'. Splatted variables
cannot be used as part of a property or array expression. Assign the result of the 
expression to a temporary variable then splat the temporary variable instead.
At line:1 char:18
    + Invoke-Expression <<<<  "bcompare @static.diff"
    + CategoryInfo          : ParserError: (:) [Invoke-Expression], ParseException
    + FullyQualifiedErrorId : NoPropertiesInSplatting,Microsoft.PowerShell.Comands.InvokeExpressionCommand

I can't get the @ to escape properly here. I've tried the `, @@, putting parts of the command in a temporary variable, but none of them did the trick.

bcompare '@static.diff'

If in doubt, put it into a string :-)

PS Home:\> args '@static.diff'
argv[0] = "C:\Users\Joey\Batches\args.cmd"
argv[1] = @static.diff

Escaping the escape character! (in invoke-expression), I am using plink in conjunction with the Invoke-Expression cmdlet. /5571579/ escaping-splatted-variables-in-powershell-invoke-expression. Greetings, I am trying to manipulate a file which I have limited access to due to limitations of the OS. I am using plink in conjunction with the Invoke-Expression cmdlet.

You need to double escape, because you are going through two levels of interpretation. Only one ` will not work because it get parsed during the string creation.

Invoke-Expression "bcompare ``@static.diff"

Or as Joey said.

Invoke-Expression "bcompare '@static.diff'"

Escape characters - PowerShell, When setting a string variable the # character does not need to be escaped, but at To have the second command run only if the first fails: Try {Command-One} Catch is also used to create arrays, create hash tables and as a splat operator. I tried replacing those out (in the variables below) with "`'" or "``'" but that didn't work either. I guess I am not sure how escaping characters works when used with the Invoke-Expression. Here are the code lines:

When I ran into the same problem, I used a backtick to make the @-sign interpreted literally. I wanted to use double-quotes for variable handling as well:

Invoke-Expression "& bcompare `@$compareCommands $file1 $file2"

Powershell Invoke-Expression and System PATH behavior, Invoke-Expression : You must provide a value expression on the taking care to escape them using the PowerShell back tick character ( ` ): In PowerShell 3.0 the special Stop Parsing symbol--% is a signal to PowerShell to stop interpreting any remaining characters on the line. This can be used to call a non-PowerShell utility and pass along some quoted parameters exactly as is. for example instead of escaping every character that PowerShell may interpret:

I was getting an error on npm install on the windows command line

The splatting operator '@' cannot be used to reference variables in an expression.

npm install @neville.dabreo/greetingbot

The splatting operator '@' cannot be used to reference variables in an expression.

But single quotes resolved the problem

npm install '@neville.dabreo/greetingbot'

NOTE - Official NPM site doesn't mention a single quotes.

Using PowerShell Invoke-Sqlcmd with Variable parameter, Learn how to use the PowerShell Invoke-Sqlcmd with the -Variable In this blog, I'll show how to use the array sub-expression operator and “splatting” It turns out that backtick is an escape character for PowerShell so that� Summary of PowerShell’s Invoke-Expression. Invoke-Expression is a wonderful way of executing a string of DOS instructions, but in PowerShell. I chose Wake-on-lan (WOL) as a vehicle to give you a grounding in this cmdlet’s syntax. The ideas is that Invoke-Expression executes the string as if you typed it at the cmd.exe command-line.

Ninja PowerShell Blog — The Ginger Ninja, Automatic variables in PowerShell are variables that store a particular state. Splatting in PowerShell is the act of taking a hashtable, and passing it as To utilize the Webhook in PowerShell, we'll make use of the Invoke-RestMethod command, To do this, we'll need to use the escape character, or `. Hello there everyone, So I have been trying to figure out without much luck how to return a variable in a remote session. Essentially I am trying to store the hostname of the remote client into a variable array if the conditions are either true or false and return them to the local session.

[CB16] Invoke-Obfuscation: PowerShell obFUsk8tion Techniques , The very best attackers often use PowerShell to hide their scripts from A/V and Can also be set with a string variable • & (Get-Command *w-O*) • . also work on PowerShell cmdlets • And so does Splatting • Once again, /c echo %cmd% ^| powershell -" Escape | with ^ for cmd.exe Does this work??? I have code that gets a bunch of filenames and zips them up using 7 zip. The problem is it doesn't work for filenames that contain single quotes, parens, brackets, etc. I

Invoke-SSHCommand -Command including Invoke-Expression , PS - In PowerShell we don't need to escape the whacks, and we don't need to I would also love a way to splat with --key value and -key=value but not sure� Escaping in PowerShell The situation is different if you are using PowerShell. You still must escape most of the characters required by Active Directory, using the backslash "\" escape character, if they appear in hard coded Distinguished Names.

  • Thanks for the clarification. Now I see the multiple passes of interpretation that were tripping me up.
  • Multiple levels of interpretation are always a pain. :)
  • That's usually the very best reason not to use Invoke-Expression. It's unnecessary here anyway.
  • Using single quotes was already proposed in two prior answers. Instead of posting a duplicate answer I think this should be a comment or, really, just a couple of upvotes on those answers.
  • Cheers, exactly what I was looking for :)