How to refresh Powershell_ise for contents modified outside the IDE.
Most of the time i would have opened both Powershell_ise and notepad++
If i does changes in Powershell_ise , notepad++ asks for reload but if i modify in notepad++ there is no way to refresh in Powershell_ise.
Whether any way to refresh the content or am i overlooking any feature which provides this?
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
This post is old, but I figured I'd post this as google brought me here with the same issue.
I eventually just wrote this little function which doesn't do exactly what the OP wanted, but maybe other googlers will find it useful:
function Build {
#Reload file
$CurrentFile = $psise.CurrentFile
$FilePath = $CurrentFile.FullPath
$PsISE.CurrentPowerShellTab.Files.remove($CurrentFile)
$PsISE.CurrentPowerShellTab.Files.add($FilePath)
iex $PsISE.CurrentPowerShellTab.Files.Editor.Text
}
$psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Clear()
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Reload file and run",{Build},'f4')
Its not perfect, but its good enough for me for now. All is does is create a key binding that closes,reopens, and then executes the current file. Its a bit jarring though because when you run it you'll lose your current cursor position when the file is closed and reopened. I'm sure you could store the column and line position of the cursor and restore it when reloading, but I'm too lazy to bother with that for the time being.
Edit: I accidentally posted an older non-working version of my code. Updated with working version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With