To complement Keith Hill's helpful answer with additional information:
Both "$computer\admin"
and the unquoted form, $computer\admin
, would work, because unquoted string arguments are implicitly treated as if they were "..."
-enclosed (double-quoted), i.e. as expandable strings that perform string interpolation (replace embedded variable references and expressions with their values), as opposed to verbatim strings ('...'
, single-quoted) that do not interpret their content.
When in doubt, use "..."
explicitly, notably when the string contains metacharacters such as |
and <
For the complete rules on how unquoted tokens are parsed as command arguments, see this answer.
Pitfalls:
The partial quoting you attempted:
'$computer'\admin
even if corrected to "$computer"\admin
to make interpolation work, would not work, because PowerShell - perhaps surprisingly - then passes the value of $computer
and verbatim string \admin
as two arguments. Only if a compound string with partial quoting starts with an unquoted string is it recognized as a single argument (e.g. $computer"\admin"
would work) - see this answer for more information.
Another notable pitfall is that only stand-alone variable references such as $computer
and $env:COMPUTERNAME
can be embedded as-is in "..."
; to embed an expression - which includes property access and indexed access - or a command, you need to enclose them in $(...)
, the subexpression operator. E.g., to embed the value of expression $someArray[0]
or $someObj.someProp
in an expandable string, you must use "$($someArray[0])"
or "$($someObj.someProp)"
- see this answer for the complete rules.