Note:
This answer shows how to escape / quote the @
char. properly in the context of PowerShell's usual parsing rules.
If your command line only contains verbatim arguments - i.e., only literal tokens, not PowerShell variable references (e.g, $file
) or expressions (e.g., ($dir + '\script.ps1')
) - you can alternatively place --%
, the stop-parsing token, before the pass-through arguments, as shown in programmer365's answer; note that cmd.exe
-style variable references such as %FOO%
are still expanded, however, and that the stop-parsing token has many limitations and pitfalls - see this answer.
@
is a metacharacter in PowerShell (a character with syntactic meaning[1]), so in order to pass it verbatim through to az
you must either quote the whole argument or `
-escape the @
individually:
With a literal script filename:
# Either: `-escape the @
az ... --scripts `@script.ps1
#`# Or: quote the whole argument
# Use '...' for a literal argument.
az ... --scripts '@script.ps1'
With the script filename stored in a variable, $file
:
# Either: `-escape the @
az ... --scripts `@$file
#`# Or: quote the whole argument
# Use "..." for an argument with variable references, i.e. an expandable string
az ... --scripts "@$file"
Note: You could get away with just @$file
in the variable case, but given that that doesn't work with any char. other than $
following the @
, it's better to get into the habit of always quoting / escaping a verbatim @
.
[1] @
has several syntactic uses, and the specific use depends on what character comes next. In your case, the @
in @script.ps1
was interpreted as the splatting operator with a variable named script
, with the .ps1
part interpreted as an attempt to access a property named ps1
on that variable - hence the error message.
$file="script.ps1"
?@$file
Just literally prints "@$File" – Tgroup@$File
works. – Tgroup@$File
happens to work, it shouldn't be relied upon, given that it breaks if@
is followed by a literal value, as in your question. Use`@$File
/`@script.ps1
or"@$File"
/'@script.ps1'
. Alternatively, to pass everything through as-is, without the ability to reference PowerShell variables, use--%
, the stop-parsing symbol operator – Dithyrambic