在PowerShell中模拟'source'命令

Ant*_*mas 1 powershell

我试图在powershell中模拟bash的命令.目的是对我进行任何更改microsoft.powershell_profile.psl并将其提供给现有的powershell实例.

以下命令在命令行中有效

$profile_content = [string]::join([environment]::newline,(get-content $profile))
invoke-expression $profile_content
Run Code Online (Sandbox Code Playgroud)

一切都很好; 我把microsoft.powershell_profile.psl它放入,它不起作用.

function source{
        $profile_content = [string]::join([environment]::newline,(get-content $args[0]))
        invoke-expression $profile_content
}
Run Code Online (Sandbox Code Playgroud)

我忽略了什么吗?

Ans*_*ers 6

您想要的内容已经内置到PowerShell中:

. C:\path\to\some.ps1
Run Code Online (Sandbox Code Playgroud)

about_Operators:

.点源运算符
在当前作用域中运行脚本,以便脚本创建的任何函数,别名和变量都添加到当前作用域.

. c:\scripts.sample.ps1
Run Code Online (Sandbox Code Playgroud)