如何删除脚本和可执行文件前面带有".\"的PowerShell要求?

Nei*_*ice 15 powershell

如何删除脚本和可执行文件前面带有".\"的PowerShell要求?

输入"a.exe"而不是".\ a.exe"时显示的PowerShell警告消息:

The command a.exe was not found, but does exist in the current location. Windows PowerShell doesn't load commands from the current location by default. If you trust this command, instead type ".\a.exe".
Run Code Online (Sandbox Code Playgroud)

man*_*lds 20

它是一种安全功能,因此您可以运行您认为正在运行的脚本.这就是为什么与cmd不同,你.在PATH中没有(当前目录)而你必须这样做.\my.exe等等.

如果您不想这样做并破坏此预防措施,请添加.到您的路径:

$env:PATH =$env:PATH+";."
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,这个技巧在最新版本中不再起作用(使用 PowerShell 7.1.3 进行测试)。一般错误消息后面是:“建议 [3,常规]:未找到命令 mycommand,但当前位置确实存在。” 默认情况下,PowerShell 不会从当前位置加载命令。如果您信任此命令,请键入:“.\mycommand”。有关更多详细信息,请参阅“get-help about_Command_Precedence”。 (3认同)