新手到PowerShell在这里。 在没有创build子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
PS C:\>. .\Example.ps1
允许我们简单地输入和制表完成Get-LocalTime
编辑注释,您可以定义一个函数,并立即在同一个脚本中使用该函数。 所以在Example.ps1
,在最后一行输入Get-LocalTime