[UDPATE – the original post had a script which did not work with CTP3 so I’ve replaced it with the correct version. Apologizes for the screw up.
I’ve updated the attached file as well. – jps]
I thought I would share my PowerShell_ISE profile with you. I haven’t done anything to clean this up or document it which I should do before sharing but I figured that it would be better to get some good examples out there and then I can clean it up later.
# I’m doing a lot of work with Modules these days so I added a drive to make it easy to
# get to.
New-PsDrive -Name Mod -PSProvider FileSystem -Root (($env:PSMODULEPATH -split “;”)[0])
Set-Alias Open PowerShell_ise
<#
This function is now built-in to the editor so it is no longer needed.
I’m keeping it here so you can see how it works.
function GoTo-Line
{
$ed = $psise.CurrentOpenedFile.Editor
[int]$l = read-host
if ($l -le $ed.LineCount)
{
$ed.SetCaretPosition($l,1)
}
else
{O
$ed.SetCaretPosition($ed.LineCount,1)
}
}
#>
function Edit-Selected
{
$ed = $psise.CurrentOpenedFile.Editor
PowerShell_ise $ed.SelectedText
}
function global:Export-SessionFiles
{
$psise.CurrentOpenedRunspace.OpenedFiles |%{
if (!$_.isSaved)
{
$title = “Save File?”
$message = “Do you want to Save `n`t$($_.FullPath)`nbefore exporting?”
$yes = New-Object System.Management.Automation.Host.ChoiceDescription “&Yes”, `
“Save $($_.FullPath).”
$no = New-Object System.Management.Automation.Host.ChoiceDescription “&No”, `
“Export but do not save $($_.fullpath).”
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
if (($host.ui.PromptForChoice($title, $message, $options, 0)) -eq 0)
{
$_.Save()
}
}
$psise.CurrentOpenedRunspace.Output.InsertText(“`nExporting $($_.FullPath)”)
$_.FullPath
} > ~/ISE-SessionFiles.txt
}
function Import-SessionFiles
{
cat ~/ISE-SessionFiles.txt | %{ $psise.CurrentOpenedRunspace.OpenedFiles.add($_) }
}
$null = $psISE.CustomMenu.Submenus.Add(“Edit Selected”, {Edit-Selected}, ‘Ctrl+E’)
$null = $psISE.CustomMenu.Submenus.Add(“Export Session Files”, {Export-SessionFiles}, ‘Ctrl+SHIFT+E’)
$null = $psISE.CustomMenu.Submenus.Add(“Import Session Files”, {Import-SessionFiles}, ‘Ctrl+SHIFT+I’)
Enjoy
Jeffrey Snover [MSFT]
Windows Management Partner Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
0 comments