windows - 与 bash 的 exec() 等效的 PowerShell 是什么?

标签 windows powershell scripting exec

这里是 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

http://i.imgur.com/17xfn6L.png

-编辑注释,您可以定义一个函数并立即在同一个脚本中使用该函数。所以在 Example.ps1 中,在最后一行输入 Get-LocalTime

关于windows - 与 bash 的 exec() 等效的 PowerShell 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34465479/

相关文章:

c++ - WriteConsoleOutputCharacterW-我的新行在哪里?

windows - 为什么 Windows 控制台会在一段时间后丢失命令行历史记录(向上箭头)?

c# - 让鼠标指针进行 super 跳跃?

python - 为什么 os.path.expanduser 不返回主目录?

c++ - windows 和 unix/linux 下的 Socket 编程混淆

json - 格式化 Invoke-RestMethod 或 ConvertFrom-Json 返回的 [pscustomobject] 实例

database - 如何使用PowerShell批量调用Update-Database

bash - "Standardized"bash 脚本的文档字符串/ self 文档

php - 表单邮件重定向问题

c++ - 如何使用 PostThreadMessage 从 C++ 关闭 Internet Explorer