[英]How do I invoke a command using a variable in powershell?
I want to invoke the shutdown.exe executable in powershell and it is located on C:\WINDOWS\System32\shutdown.exe
.我想调用 powershell 中的 shutdown.exe 可执行文件,它位于
C:\WINDOWS\System32\shutdown.exe
上。
Since we already got the C:\WINDOWS
in the variable $env:windir
I want to just use it and concatenate the rest of the path into the command.由于我们已经在变量
$env:windir
中获得了C:\WINDOWS
我只想使用它并将路径的 rest 连接到命令中。 I was trying this:我正在尝试这个:
PS C:\Users\shina> .\$env:windir\System32\shutdown.exe -s
But I got the following error:但我收到以下错误:
.\$env:windir\System32\shutdown.exe: The term '.\$env:windir\System32\shutdown.exe' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
How can I accomplish this?我怎样才能做到这一点?
You can use:您可以使用:
& $env:windir\System32\shutdown.exe -s
Or或者
. $env:windir\System32\shutdown.exe -s
(note the space). (注意空格)。
&
is the call operator. &
是呼叫运算符。 You can read more about it here :你可以在这里阅读更多关于它的信息:
Runs a command, script, or script block.
运行命令、脚本或脚本块。 The call operator, also known as the "invocation operator", lets you run commands that are stored in variables and represented by strings or script blocks.
调用运算符,也称为“调用运算符”,可让您运行存储在变量中并由字符串或脚本块表示的命令。
.
is the dot sourcing operator.是点源运营商。 You can read more about it here :
你可以在这里阅读更多关于它的信息:
Runs a script in the current scope so that any functions, aliases, and variables that the script creates are added to the current scope, overriding existing ones.
在当前 scope 中运行脚本,以便将脚本创建的任何函数、别名和变量添加到当前 scope 中,覆盖现有的。
The dot sourcing operator is followed by a space.
点源运算符后跟一个空格。 Use the space to distinguish the dot from the dot (.) symbol that represents the current directory.
使用空格将点与表示当前目录的点 (.) 符号区分开来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.