57

I have been tweaking some of the scripts in my PowerShell profile and it has got annoying to exit powershell then restart it so it will load any changes I have made to the scripts in my profile. Is it possible to restart the powershell session without exiting?

shreddish
  • 1,637
  • 8
  • 24
  • 38

8 Answers8

27

You can just do . $profile to source the profile again.

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • 1
    Or specify a profile file if you are updating something other than C:\Users\wombat\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 – EBGreen Jul 18 '12 at 16:43
  • 7
    The term 'C:\Users\Documents\WindowsPowerShell\Microsoft.PowerShell_ profile.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:2 + . <<<< $profile + CategoryInfo : ObjectNotFound: (C:\Users\nrp023...ell_profile.p s1:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException – shreddish Jul 18 '12 at 18:49
  • 1
    When you say that you are tweaking some of the scripts in you profile, what file exactly are you tweaking? – EBGreen Jul 18 '12 at 18:51
  • 2
    This doesnt in any way reset the actual session variables, modules loaded etc. downvoted. – Casper Leon Nielsen Oct 21 '15 at 11:32
  • @shreddish you need to create the `$profile` first, you can do it by executing `new-item -path $profile -itemtype file` in powershell. At least I think that's the problem. – mooper May 10 '17 at 07:42
  • 5
    This will not refresh the session. Any variables you defined will still exist from previous commands. – Kellen Stuart Aug 15 '18 at 20:10
16

This will start a new session, reloading your profile, in the same console window:

Invoke-Command { & "powershell.exe" } -NoNewScope # PowerShell 5
Invoke-Command { & "pwsh.exe"       } -NoNewScope # PowerShell 7

Or a longer but more robust command, the following should work correctly across versions:

Get-Process -Id $PID | Select-Object -ExpandProperty Path | ForEach-Object { Invoke-Command { & "$_" } -NoNewScope }

You will, of course, lose all your variables etc. from the previous session.

Note: creating new sessions in the same window like this will leave the parent process running in the background. Generally, this is not a problem as the parent (and any other ancestors) will terminate when the window is closed. If this is a concern, however, you may want to add something like the following to your profile:

$p = Get-Process -Id $PID
If ($p.Parent.Name -eq $p.Name -and !($p.MainWindowTitle))
{
    Stop-Process -Id $p.Parent.Id -Force
}
nmbell
  • 451
  • 3
  • 7
  • 1
    doesn't reload environment variables – Siraj Kakeh Apr 05 '20 at 15:21
  • Using this command in PowerShell7(Windows Terminal) will go back to PowerShell5. – Andy Oct 15 '20 at 04:57
  • @if_ok_button For PowerShell 7, you'll need to use the [PowerShell 7 exe](https://learn.microsoft.com/en-us/powershell/scripting/install/migrating-from-windows-powershell-51-to-powershell-7?view=powershell-7#separate-installation-path-and-executable-name). – nmbell Oct 18 '20 at 16:33
  • @if_ok_button Works fine for me in VS Code in both powershell and Powershell Integrated Console terminals... – nmbell Oct 18 '20 at 16:34
  • When I try this command (either from within the editor or pasting it into the console), it 'hangs' the PowerShell ISE (aka the run button is greyed out & the stop button is red). – George 2.0 Hope Feb 25 '21 at 13:53
  • @George 2.0 Hope Unlike with VS Code, the command window in Powershell ISE isn't a separate process, so you're going to have a hard time starting a new session without opening a new instance. [The PowerShell ISE is no longer in active feature development](https://learn.microsoft.com/en-us/powershell/scripting/windows-powershell/ise/introducing-the-windows-powershell-ise?view=powershell-7.1#support), so don't hold your breath on this changing any time soon. – nmbell Feb 27 '21 at 18:04
  • I found this because I was trying to create an alias in my `profile.ps1` file that would reload my profile when I made changes to it. This is the only solution I found that would run from within an alias that would update and reload the session. Upvoted, thanks so much! – Gharbad The Weak Mar 11 '22 at 18:14
  • I just get a new pwsh **on top of the previous one**, I can just `exit` to the previous one. – c z Nov 28 '22 at 11:48
7

@manojlds' answer is right, but it could end up throwing errors. For example, if you've defined a new PSDrive in your profile, then re-dotsourcing it may cause errors.

An alternative approach is to first start powershell, then immediately start another version inside by just typing PowerShell. I make the changes to my profile in the nested console, exit, then rerun PowerShell to test the updated profile.

Another thing - make profile changes slowly and carefully. In my view, while profiles do need to evolve, that evolution typically should be slow. YMMV!!

Thomas Lee
  • 1,158
  • 6
  • 13
5

You can make a simple function and add it to your $profile (or make a module and then import into '$profile'), for example:

function Restart-PowerShell{
    Start-Process PowerShell # Launch PowerShell host in new window
    exit # Exit existing PowerShell host window
}
# Add any alias if you want, for ex. rps (rp already occupied by "Remove-ItemProperty”)
Set-Alias -Name rps -Value Restart-PowerShell

Note: While this may not be exactly what you wrote in the title of your question "w/out exiting", but if, in substance, your original intention was to have some kind of restart PowerShell host command (which is not available in PS by default) then it will do the work...

Bad
  • 4,967
  • 4
  • 34
  • 50
  • 4
    `start powershell` is the real answer here, though note that you will lose your window settings. – Andrew Nov 02 '17 at 17:49
  • 1
    To close the old shell & start a new shell type "exit start powershell" however as Andrew noted previous settings will not presisit. – cyber101 Jan 07 '21 at 20:13
1

Here is an expansion upon @Bad's answer. This will now handle ISE (because I use it all the time).

The idea is if you are in ISE, it will restart ISE. If you are in plain PowerShell, it will restart plain PowerShell. Get-PSHostProcessInfo returns information about the current PowerShell session which allows you to determine whether or not you are running ISE or not.

function Restart-PowerShell
{
    if((Get-PSHostProcessInfo).ProcessName.Contains('powershell_ise'))
    {
        Start-Process 'powershell_ise.exe'
    } else {
        Start-Process 'powershell.exe'
    }
    exit
}
Kellen Stuart
  • 7,775
  • 7
  • 59
  • 82
  • Get-PSHostProcessInfo returns info for all currently running instances, so this could be wrong if you are calling it from the plain console vs the ISE. You might think about using $host.Name instead, which should return the process name you are actively using. Something like: `if ((Get-Process -id $pid).ProcessName.contains("powershell_ise"))...`, or a little differently like this: `if ($host.Name -imatch "ise")...` – Randall Borck Mar 08 '19 at 23:08
  • @RandallBorck Thanks for the advice! You can edit my answer if you have tested this – Kellen Stuart Mar 09 '19 at 01:12
0

In profile.ps1 paste this. Simply type reload and and it will simply reload / restart.

function reload-Console {
    clear
    Write-Host "Reload Console"
    Get-Process -Id $PID | Select-Object -ExpandProperty Path | ForEach-Object { Invoke-Command { & "$_" } -NoNewScope }
}
New-Alias reload reload-Console
Adrian
  • 2,273
  • 4
  • 38
  • 78
0

Note that manojlds' answer (the current top rated answer) is possibly problematic depending on what your profile is doing (essentially: if it depends on a clean slate to start). But I think the biggest issue with it is that it is not easy to put into a function which can be aliased.

So, I prefer something similar to nmbell's answer, but much simpler:

function Reset-Session {
    # store this shell's parent PID for later use
    $parentPID = $PID
    # get the the path of this shell's executable
    $thisExePath = (Get-Process -Id $PID).Path
    # start a new shell, same window
    Start-Process $thisExePath -NoNewWindow
    # stop this shell if it's still alive
    Stop-Process -Id $parentPID -Force
}
# optional
Set-Alias -Name reset -Value Reset-Session
bzm3r
  • 3,113
  • 6
  • 34
  • 67
-1

refreshenv

Executed code example

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 22 '22 at 14:05
  • Refreshenv is not built-in, it's part of Chocolatey. – Pxtl Mar 22 '23 at 19:32