PowerShell ISE Can Do a Lot More Than You Think

In CTP2, we released the first version of PowerShell_ISE.  It was VERY rough and primitive and we came close to deciding to wait to CTP3 to ship it.  We decided to go head and ship it in CTP2 because the team got it to critical mass and we wanted to let the world know where we were going as soon as possible and get their feedback.

It’s been a long time since CTP2 and with CTP3, I think you’ll find a MUCH richer, MUCH more useful PowerShell_ISE.  I strongly encourage you to spend some time exploring it.  It supports a ton of new features (my favorite is graphical debugging).

After you’ve explored all the menu items and gotten a good idea of what it can do, you are ready to start partying because that is just the beginning of what PowerShell_ISE can do.  The reason for that is that PowerShell_ISE is programmable.

We don’t have good documentation on the programming model at this point.  That may or may not change by the time we ship V1 (because we have a TON of things to document and ‘to ship is to choose’) but it shouldn’t matter if you’ve embraced the PowerShell model of education through investigation.  With Get-Member (gm) – you can figure it all out yourself.  All you need is a starting point.  That staring point is $psIse.

PS Mod:\form> $psIse |GM

   TypeName: System.Management.Automation.Host.PSGHost

Name                  MemberType Definition                                                                      
—-                  ———- ———-                                                                      
Equals                Method     bool Equals(System.Object obj)                                                  
GetHashCode           Method     int GetHashCode()                                                               
GetType               Method     type GetType()                                                                  
ToString              Method     string ToString()                                                               
CurrentOpenedFile     Property   System.Management.Automation.Host.OpenedFile CurrentOpenedFile {get;}           
CurrentOpenedRunspace Property   System.Management.Automation.Host.OpenedRunspace CurrentOpenedRunspace {get;}   
OpenedRunspaces       Property   System.Management.Automation.Host.OpenedRunspaceCollection OpenedRunspaces {get;}
Options               Property   System.Management.Automation.Host.Options Options {get;}                        

PS Mod:\form> $psIse.CurrentOpenedRunspace

DisplayName : PowerShell 1
ToolsMenu   : System.Management.Automation.Host.PSMenuItem
StatusText  : Running script / selection.  Press Ctrl-Break to stop.
Prompt      : PS Mod:\form>
CommandPane : Microsoft.Windows.PowerShell.Gui.Internal.CommandEditor
Output      : Microsoft.Windows.PowerShell.Gui.Internal.OutputEditor
OpenedFiles : {Untitled1.ps1, Form.psm1, Untitled3.ps1*, Microsoft.PowerShellISE_profile.ps1…}
CanExecute  : False

PS Mod:\form> $psIse.CurrentOpenedRunspace |GM

   TypeName: System.Management.Automation.Host.OpenedRunspace

Name            MemberType Definition                                                                                                      
—-            ———- ———-                                                                                                      
PropertyChanged Event      System.ComponentModel.PropertyChangedEventHandler PropertyChanged(System.Object, System.ComponentModel.Propert…
Equals          Method     bool Equals(System.Object obj)                                                                                  
Execute         Method     System.Void Execute(string script)                                                                              
GetHashCode     Method     int GetHashCode()                                                                                               
GetType         Method     type GetType()                                                                                                  
ToString        Method     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;}                                                                                 
ToolsMenu       Property   System.Management.Automation.Host.PSMenuItem ToolsMenu {get;}                                                   

PS Mod:\form> # Output is obviously the Output window
PS Mod:\form> $psIse.CurrentOpenedRunspace.output |gm

   TypeName: Microsoft.Windows.PowerShell.Gui.Internal.OutputEditor

Name             MemberType Definition                                                                                                     
—-             ———- ———-                                                                                                     
PropertyChanged  Event      System.ComponentModel.PropertyChangedEventHandler PropertyChanged(System.Object, System.ComponentModel.Proper…
Clear            Method     System.Void Clear()                                                                                            
EnsureVisible    Method     System.Void EnsureVisible(int lineNumber)                                                                      
Equals           Method     bool Equals(System.Object obj)                                                                                 
Focus            Method     System.Void Focus()                                                                                            
GetHashCode      Method     int GetHashCode()                                                                                              
GetLineLength    Method     int GetLineLength(int lineNumber)                                                                              
GetType          Method     type GetType()                                                                                                 
InsertText       Method     System.Void InsertText(string text)                                                                            
Select           Method     System.Void Select(int startLine, int startColumn, int endLine, int endColumn)                                 
SetCaretPosition Method     System.Void SetCaretPosition(int lineNumber, int columnNumber)                                                 
ToString         Method     string ToString()                                                                                              
CaretColumn      Property   System.Int32 CaretColumn {get;}                                                                                
CaretLine        Property   System.Int32 CaretLine {get;}                                                                                  
LineCount        Property   System.Int32 LineCount {get;}                                                                                  
SelectedText     Property   System.String SelectedText {get;}                                                                              
Text             Property   System.String Text {get;set;} 



PS Mod:\form> # Now let’s select the output for all lines that have
PS Mod:\form> # “Event” in them
PS Mod:\form> $psIse.CurrentOpenedRunspace.output.Text -Split “`n” |Select-String Event

PropertyChanged Event      System.ComponentModel.PropertyChangedEventHandler PropertyChanged(System.Object, System.ComponentModel.Propert…
PropertyChanged  Event      System.ComponentModel.PropertyChangedEventHandler PropertyChanged(System.Object, System.ComponentModel.Proper…
$psIse.CurrentOpenedRunspace.output.Text -Split “`n” |Select-String Event


So you can see from this thread that using what you already know about PowerShell, you can investigate the system and figure things out for yourself.  Once you figure something out – post it so that the rest of us can benefit from your exploration.  Remember, you can extend your environment by putting things into your profile.  Also remember that each host has its OWN profile so PowerShell_ISE has a different profile from PowerShell.exe.  You get at it via $PROFILE. 

Here is what I just put into my profile. Notice that I added it as a menu item as well.


Function Select-Output
[Parameter(Position = 0, Mandatory = $true,
HelpMessage=”Text to select from the Output Window”)]

[Parameter(Position = 1)]
     $psIse.CurrentOpenedRunspace.output.Text -Split “`n” |Select-String -Context:$Context -Pattern:$Pattern
$null = $psISE.CurrentOpenedRunspace.ToolsMenu.Submenus.Add(“Select From Output”, {Select-Output}, $null)


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