Here is a different spin on red888's script:
function Reload {
$CurrentFile = $psise.CurrentFile
$FilePath = $CurrentFile.FullPath
$lineNum = $psise.CurrentFile.Editor.CaretLine
$colNum = $psise.CurrentFile.Editor.CaretColumn
$PsISE.CurrentPowerShellTab.Files.remove($CurrentFile) > $null
$newFile = $PsISE.CurrentPowerShellTab.Files.add($FilePath)
$newfile.Editor.SetCaretPosition($lineNum,$colNum)
}
$psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Clear()
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Reload File",{Reload},'f4') > $null
It restores the position of the caret after reload. I removed the line
iex $PsISE.CurrentPowerShellTab.Files.Editor.Text
As I didn't need it and its also not the same as running the script (and so results in strange behavior of statements like $script:MyInvocation.MyCommand.Path
).
Incidentally, if you put this code in your ISE profile it will automatically run when you first load the ISE. The ISE profile is just a powershell script whose location is given by the $profile
variable.
Here are some commands that create the profile if it doesn't exist, and then opens it. Run it from inside the ISE:
if (!(Test-Path (Split-Path $profile))) { mkdir (Split-Path $profile) } ;
if (!(Test-Path $profile)) { New-Item $profile -ItemType file } ;
notepad $profile