且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从PowerShell中的脚本文件重新加载用户配置文件

更新时间:2022-12-07 08:07:01

因此,您标记为答案的方法可能在Powershell命令提示符下有效,但在PowerShell ISE中无效(对于我来说,提供了出色的PowerShell会话),可能无法在其他PowerShell环境中正常工作.

So, the approach that you marked as the answer may work inside the Powershell command prompt, but it doesn't work inside PowerShell ISE (which, to me, provides a superior PowerShell session) and probably won't work right in other PowerShell environments.

这是一个我已经使用了一段时间的脚本,它在任何环境下都对我非常有效.我只是将此函数放在〜\ Documents \ WindowsPowerShell中的Profile.ps1中,每当我要重新加载我的概要文件时,我都会将该函数点源化,即

Here's a script that I have been using for a while, and it has worked very well for me in every environment. I simply put this function into my Profile.ps1 at ~\Documents\WindowsPowerShell, and whenever I want to reload my profile, I dot-source the function, i.e.

. Reload-Profile

功能如下:

function Reload-Profile {
    @(
        $Profile.AllUsersAllHosts,
        $Profile.AllUsersCurrentHost,
        $Profile.CurrentUserAllHosts,
        $Profile.CurrentUserCurrentHost
    ) | % {
        if(Test-Path $_){
            Write-Verbose "Running $_"
            . $_
        }
    }    
}