#PSTip Selecting/copying the current line in PowerShell ISE

Windows PowerShell Integrated Scripting Environment (ISE) is my first preferred script editor. Especially, with all the new cool features introduced in version 3.0. I love working with ISE and try to make my life easier when using the same. Fortunately, ISE exposes the scripting object model — $psISE. This object model makes it easy to extend ISE functionality and this tip is about one of those aspects.

Very often, I end up selecting and copying the current line from ISE script editor to a console prompt or into a document or email I am working on. If you have looked at ISE menu or keyboard shortcuts closely, there is no keyboard shortcut or ISE menu item to select current line.

This is where $psISE kicks in.

In the ISE scripting object model, the current line is represented by the property called CaretLine. This can be accessed using:


Now, using the same object model, we can select the current line using :


Now, coming to the fun part of adding this as a menu item — once again using $psISE scripting object model:

$ScriptBlock = {

$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Select _Current Line", $ScriptBlock, "Ctrl+Alt+C")

This is it. Once you execute the above code snippet and press Ctrl+Alt+C, the current line in the script is selected. Now, extending this to copy the current line isn’t tough. Let us see that as well.

$ScriptBlock = {
 $psISE.CurrentFile.Editor.SelectedText | clip

$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Copy _Current Line", $ScriptBlock, "Alt+C")

Simple! Isn’t it?

About the author: Ravikanth C

Ravikanth is the founder and editor of the PowerShell Magazine. He is also a PowerShell MVP who works at Dell Inc. He blogs at http://www.ravichaganti.com/blog and you can follow him on Twitter @ravikanth.

Related Posts

%d bloggers like this: