执行脚本时使用$ env:用户名或其他变量
因此,我一直在PowerShell上编写脚本,我想进行一些更改,以使人们能够以最小的努力使用它。其中一部分包括调整脚本,以便我的用户名的实例被$ env:用户名
替换。
例如,c:\ users \ {userName} \ downloads \ reputable.exe.exe
将变成c:\ users \ $ env:username \ username \ downloads \ exparable.ecutable.exe.exe
和the结果将是相同的。
我反复获得错误术语'c:\ users \ $ env:用户名\ downloads \ exputable.exe''未被识别为cmdlet,函数,脚本文件或可操作程序的名称。检查名称的拼写,或者如果包括路径,请验证该路径是否正确,然后重试。
作为分配变量的一部分调用时,我会得到术语C:\用户\ {username} \ downloads \ reputable.exe不是...
,因此在这种情况下,该路径是正确处理的。
我在bash中使用了0期,我在bash中使用了变量,所以我不确定我的出错了。
我尝试将其分配给变量并调用它,并分配$ env:用户名
,并将该变量包括在路径中,但都没有起作用。
我也尝试使用$ env:homepath
。
我尝试将变量分配为字符串(带有双引号),并使用invoke-command $ command
和invoke-expression $命令
,但没有骰子。
如何在路径名中执行具有变量的脚本或可执行文件?这在Powershell中可以吗?
Mathias R. Jensen的答案解决了我的问题,我的代码现在有效。
#Constructed path as advised
$TestPath = Join-Path $env:USERPROFILE \Downloads\executable.exe
Get-date | Out-File -FilePath $env:USERPROFILE\test\testfile.txt -Append
#Invoked executable
& $TestPath | Out-File -FilePath $env:USERPROFILE\test\testfile.txt -Append
So I've been writing a script in powershell, and I'd like to make a few alterations to allow people to use it with minimal effort on their part. Part of this includes adjusting the script so that instances of my username in paths are replaced with $env:USERNAME
.
For example, C:\Users\{username}\Downloads\executable.exe
would become C:\Users\$env:USERNAME\Downloads\executable.exe
and the outcome would be the same.
I repeatedly get the error The term 'C:\Users\$env:USERNAME\Downloads\executable.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
When I calling it as part of an assigned variable, I get The term C:\Users\{username}\Downloads\executable.exe is not ...
, so the path is processed correctly in this case.
I've used variables in paths in bash before with 0 issue, so I'm unsure of where I'm going wrong.
I've tried assigning it to a variable and calling it, and assigning $env:USERNAME
and including that variable in the path but neither worked.
I've tried using $env:HOMEPATH
too.
I've tried assigning to a variable as a string (with double quotes), and using Invoke-command $command
and Invoke-Expression $command
but no dice.
How would I execute a script or an executable with a variable in the path name? Is this possible in powershell?
Mathias R. Jensen's answer fixed my issue, my code now works.
#Constructed path as advised
$TestPath = Join-Path $env:USERPROFILE \Downloads\executable.exe
Get-date | Out-File -FilePath $env:USERPROFILE\test\testfile.txt -Append
#Invoked executable
& $TestPath | Out-File -FilePath $env:USERPROFILE\test\testfile.txt -Append
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于相对于用户配置文件夹的构建路径,请使用
$ env:userProfile
,然后将操作分为两个单独的步骤:&
call operator :For constructing paths relative to the users profile folder, use
$env:USERPROFILE
, and split the operation into 2 separate steps:&
call operator: