Expand string Variable stored via Single Quote in Powershell

不想你离开。 提交于 2019-12-17 20:56:14

问题


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?


回答1:


Expand the string on the remote side:

$ExecutionContext.InvokeCommand.ExpandString($RemotePath)



回答2:


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



来源:https://stackoverflow.com/questions/27226606/expand-string-variable-stored-via-single-quote-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!