powershellprofile

How to reload user profile from script file in PowerShell


I want to reload my user profile from a script file. I thought that dot sourcing it from within the script file would do the trick, but it doesn't work:

# file.ps1
. $PROFILE

However, it does work if I dot source it from PowerShell's interpreter.

Why do I want to do this?

I run this script every time I update my profile and want to test it, so I'd like to avoid having to restart PowerShell to refresh the environment.


Solution

  • 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.

    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
    

    Here's the function:

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