Weekend Scripter: Copy Text from One Tab in the PowerShell ISE to the Next

Summary: Microsoft Scripting Guy, Ed Wilson, creates a function to copy script text from one Windows PowerShell ISE tab to a new one.

Microsoft Scripting Guy, Ed Wilson, is here. Well, the day is finally here. It is time for the Jacksonville, Florida IT Pro Camp. The speaker dinner last night was great, and the Scripting Wife and I made new friends. Jacksonville, Florida is a great town, although it has been more than 20 years since I used to live here. Interestingly enough, my favorite independent bookstore still exists, and in fact, it is thriving. The Scripting Wife and I spent several hours there yesterday just before the speaker dinner.

Creating a copy script from one tab to the other function

One of the cool things about the Windows PowerShell ISE is the object model that permits easy modification and extension of functionality. When I am working on a script, I often copy a portion of code to a new tab in the Windows PowerShell ISE so I can isolate and fix a particular issue. Another reason I find myself copying code from one tab to a new tab in the Windows PowerShell ISE is because I am extending the functionality of a script and I do not want mess up my original script. (Of course, a real source control program solves this particular problem).

Now, to be honest, it is not very difficult to copy code form one tab to another tab. I just use Control+A (ctrl-A) to highlight all the code, and Control+V (ctrl-V) to paste it in the new script tab after I have used Control+N (ctrl-N) to create a new script tab. But let’s see…that is at least six key strokes, and I have to think about it, and remember three different key stroke combinations. If I want to copy a selection of script instead of the entire script, it is even more work. The Copy-ScriptToNewTab function makes this a bit easier by creating a function that will copy an entire script from the current script pane to a new script pane (tab).

A switched parameter also permits copying only the selected text to the new script pane (tab). I add an alias (cs) to make it easier to use this function. To ensure that the function is always available, I add it to my Windows PowerShell ISE profile. I also add the cs alias into my Windows PowerShell ISE profile. I used the Add-HeaderToScript function from my Windows PowerShell ISE profile to add a header to the script file, and also the Add-Help function to add comment-based Help to the Copy-ScriptToNewTab function. The parameter portion of the script is simple, and it is shown here.


Next I check to see if the Selection switched parameter exists. If it does, I first create a new tab in the Windows PowerShell ISE. Then I set the text for the new tab to equal the selected text. Here is the code that accomplishes these two tasks.


   { $newtab = $psISE.CurrentPowerShellTab.Files.Add()

     $newtab.Editor.Text = $psise.CurrentFile.Editor.SelectedText }

If the Selection switched parameter does not exit, I copy the entire script text. Note that there is only a single word difference between the two commands. The command to add a new tab to the Windows PowerShell ISE calls the Add method from the Files object from the CurrentPowershellTab. If I want only selected text, I use the SelectedText property. If I want the entire script text from the CurrentFileEditor object, I use the Text property. This portion of the function is shown here.


   { $newtab = $psISE.CurrentPowerShellTab.Files.Add()

     $newtab.Editor.Text = $psISE.CurrentFile.Editor.text }

That is it really. There are only two basic lines. The remainder of the function is a little bit of structure, and of course the comment-based Help. I do not really need the comment-based Help for myself—it is a pretty basic function. But I thought I would add it because I am sharing the function. Besides, with the Add-Help function, it takes me less than a minute to add comment-based Help. Use of the function is shown in the following image.

Image of function

The complete function is shown here.


Function Copy-ScriptToNewTab




    This does that


    This function does



    Example- accomplishes


    The parameter


    NAME:  Example-

    AUTHOR: ed wilson, msft

    LASTEDIT: 06/10/2012 10:00:40





 #Requires -Version 2.0




   { $newtab = $psISE.CurrentPowerShellTab.Files.Add()

     $newtab.Editor.Text = $psise.CurrentFile.Editor.SelectedText }


   { $newtab = $psISE.CurrentPowerShellTab.Files.Add()

     $newtab.Editor.Text = $psISE.CurrentFile.Editor.text }

} #end function Copy-ScriptToNewTab

Like I said, to make using the function easier, I added it to my Windows PowerShell ISE profile, and I created an alias to it. That’s about it. Join me tomorrow for more Windows PowerShell cool stuff. And if you happen to be in Jacksonville at the IT Pro Camp, come up and say, “Hi.” We hope to see you.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy