Automating the world one-liner at a time…
[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
Great posts on the ISE. Thanks for letting us stand on your shoulders.
I used the new ISE for debugging how I was using James Brundage's scripts he posted.
Super helpful!
The way you are adding menus to the ISE looks a bit similar how you might do it with Oslo's Intellipad. Are you guys using the Managed Extensibility Framework (MEF) too?
> Are you guys using the Managed Extensibility Framework (MEF) too?
No - MEF wasn't ready in time for us.
jps
I have several posts some customizations you can do with the ISE using $PsIse at http://get-powershell.com. It really is pretty remarkable what you can do.
I don't have a ToolsMenu.
But I do have a $psISE.CustomMenu I can add Submenus to.
PS C:\> $psISE.CurrentOpenedRunspace
DisplayName : PowerShell 1
StatusText : Running
Prompt : PS C:\>
CommandPane : Microsoft.Windows.PowerShell.Gui.Internal.CommandEditor
Output : Microsoft.Windows.PowerShell.Gui.Internal.OutputEditor
OpenedFiles : {Untitled1.ps1}
CanExecute : False
PS C:\> $psISE.CurrentOpenedRunspace |gm
TypeName: System.Management.Automation.Host.OpenedRunspace
Name MemberType Definition
---- ---------- ----------
PropertyChanged Event System.ComponentModel.PropertyChangedEventHandler PropertyChanged(System.Object, System.ComponentModel.PropertyChangedEventArgs)
Equals Method System.Boolean Equals(Object obj)
Execute Method System.Void Execute(String script)
GetHashCode Method System.Int32 GetHashCode()
GetType Method System.Type GetType()
ToString Method System.String ToString()
CanExecute Property System.Boolean CanExecute {get;}
CommandPane Property System.Management.Automation.Host.EditorBase CommandPane {get;}
DisplayName Property System.String DisplayName {get;set;}
OpenedFiles Property System.Management.Automation.Host.OpenedFileCollection OpenedFiles {get;}
Output Property System.Management.Automation.Host.EditorBase Output {get;}
Prompt Property System.String Prompt {get;}
StatusText Property System.String StatusText {get;}
The Windows PowerShell Integrated Scripting Environment (ISE) is a host application that enables you
Few things to correct...
As Doug said $psISE.CurrentOpenedRunspace.ToolsMenu.Submenus.Add should be $psise.CustomMenu.Submenus.Add
"Go to Line" (Ctrl+G) is already present as an entry under Edit menu, so it's not possible to add another one under Custom menu.
Also, the attached file is missing Select-Output function.
20 points for publishing your profile.
70 points for "just doing it" instead of postponing it forever because you hadn't cleaned it up yet!
"As soon as I clean it up" is the number one enemy of code sharing, thanks for not falling victim. :)
Oh, and a bonus 20 points for having cool stuff in there I hadn't thought of yet ;)
Thanks for posting this. It looks like you're working with a newer version than CTP3 already since you're modifying a Tools Menu rather than the Custom menu. I particularly like the Export-SessionFiles and Import-SessionFiles functions, but then I appreciate seeing Select-Output as well.
Wow, looks great!
I do have a question, though.
I've been digging through the $psISE.Options and customizing PowerShellISE to have a black background and other color customizations. Is there a way to customize the text caret color?
For example, if you've changed the background color to black, you now can't see the text caret unless you have a way to change its color, too.
Thanks for a great product!
Few questions:
-Can indention be changed programmatically?
-How do I turn off the interactive portions of ISE (aka: I want a bare "notepad+" screen where I can simply write scripts). I don't see any options for hiding the interactive bits
-Similar to the last question, any way to hack ISE to hide the icons--great for UI ppl but I don't ever use them as I like to type and use keyboard shortcuts :)
@vivek
- Indention. Depends what you want to do. Before we had TAB and SHIFT-TAB to indent/un-indent code, we wrote scripts to do the same thing. What we would do is to select the section we want to work on (This is available as $psISE.CurrentOpenedFile.Editor.SelectedText). You can then do whatever you want to that string and then replace the selected text by using: $psISE.CurrentOpenedFile.Editor.InsertText("Replacement-text")
Experiment with that - I think you'll like it.
If you were asking for a way to say something like "When I hit tab, I want it to mean 8 spaces" - we don't have that.
-----------------------
RE: Turning of interactive portions of the ISE and hiding icons. We are doing a number of changes to the UI layout to increase the amount of visual workspace we provide. We are experimenting with a few ideas so nothing is settled yet.
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
@Vivek
If you really just want a bare bones notepad+, you can put the script pane on the right (ctrl + shift + s) and drag the splitter bar all the way to the left.
However, I personally like having the interactive part up. You can highlight a part of your script and press F5 and only run that part and the result is displayed in the output window. But granted, this works better on a large wide screen monitor.
thanks
Christian [MSFT]
UX Designer
This script no longer works on the RTM Versions of PowerShell V2. The reason is a late breaking change in handling add-in menus.
The lines that read:
$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')
Should Read:
$null = $psISE.CurrentPowerShellTab.AddOnsMenu.submenus.Add("Edit Selected",{edit-selected}, 'Ctrl+E')
$null = $psISE.CurrentPowerShellTab.AddOnsMenu.submenus.Add("Export Session Files", {Export-SessionFiles}, 'Ctrl+Alt+E')
$null = $psISE.CurrentPowerShellTab.AddOnsMenu.submenus.Add("Import Session Files", {Import-SessionFiles}, 'Ctrl+Alt+I')
Hopefully Jeffrey or whoever will fix the post - but in the meantime, the fix is in this comment! :-)
Did I mention that ISE rocks?