Expand string Variable stored via Single Quote in Powershell

3.3k views Asked by At

I have a scenario where I need to construct a powershell path as $RemotePath = '$($env:USERPROFILE)\Desktop\Shell.lnk'. This variable gets passed to a remote machine where it needs to be executed. The remote machine receives this as a string variable. How do I expand the string to evaluate $env:USERPROFILE?

2

There are 2 answers

1
Ansgar Wiechers On BEST ANSWER

Expand the string on the remote side:

$ExecutionContext.InvokeCommand.ExpandString($RemotePath)
3
ojk On

By using a double quotes. PowerShell won't expand variables inside single-quoted strings.