这里是 PowerShell 的新手。在不创建子 shell 的情况下从当前进程中运行新脚本的最佳方法是什么?在 Bash 中,您可以通过以下方式执行此操作:
source script # Executes the commands in script within the current shell
或
exec script # Same as source, except it completely replaces the current process with script
PowerShell 中是否有相应的等效项?
最佳答案
你也可以使用.\Script.ps1
或者开始键入名称并使用 TAB
来完成脚本名称。
点源脚本允许函数在命令行上可用。
例如:
Function Get-LocalTime {
$CurTime = Get-Date
"The Date and time currently is $CurTime"
}
现在我们点源(将上面的脚本命名为 Example.ps1)PS C:\>。 .\Example.ps1
允许我们简单地键入和 Tab 完成 Get-LocalTime
-编辑注释,您可以定义一个函数并立即在同一个脚本中使用该函数。所以在 Example.ps1
中,在最后一行输入 Get-LocalTime
关于windows - 与 bash 的 exec() 等效的 PowerShell 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34465479/