Powershell 使用 ENV 变量转换字符串 [英] Powershell Convert String With ENV Variable

查看:30
本文介绍了Powershell 使用 ENV 变量转换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

$myVar = 'C:\Users\$env:USERNAME\Desktop'
Test-Path "$myVar"

变量 $myVar 需要带单引号来显示用户使用的实际变量.之后我如何才能将变量实际处理到要使用的实际路径中?

The variable $myVar is needed with single quotes to display the actual variables used to the user. How can I then actually process the variable later into the actual path to use?

Test-Path $myVar 返回 False,没有看到用户路径 "C:\Users\User\Desktop"

Test-Path $myVar returns False, not seeing the users path "C:\Users\User\Desktop"

推荐答案

找到了!由于无法更改原始变量 $myVar 中的单引号,因此稍后可以使用以下方法扩展其变量:

Found it! As the single quotes could not be changed in the original variable $myVar, the following worked to expand its variable later:

$ExecutionContext.InvokeCommand.ExpandString($myVar)

这篇关于Powershell 使用 ENV 变量转换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆