{"id":9031,"date":"2012-06-16T00:01:00","date_gmt":"2012-06-16T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/06\/16\/weekend-scripter-copy-text-from-one-tab-in-the-powershell-ise-to-the-next\/"},"modified":"2012-06-16T00:01:00","modified_gmt":"2012-06-16T00:01:00","slug":"weekend-scripter-copy-text-from-one-tab-in-the-powershell-ise-to-the-next","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-copy-text-from-one-tab-in-the-powershell-ise-to-the-next\/","title":{"rendered":"Weekend Scripter: Copy Text from One Tab in the PowerShell ISE to the Next"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, creates a function to copy script text from one Windows PowerShell ISE tab to a new one.<\/p>\n<\/p>\n<p>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.<\/p>\n<\/p>\n<h2>Creating a copy script from one tab to the other function<\/h2>\n<\/p>\n<p>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).<\/p>\n<\/p>\n<p>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&rsquo;s see&hellip;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 <b>Copy-ScriptToNewTab<\/b> 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).<\/p>\n<\/p>\n<p>A switched parameter also permits copying only the selected text to the new script pane (tab). I add an alias <b>(cs) <\/b>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 <b>cs<\/b> alias into my Windows PowerShell ISE profile. I used the <b>Add-HeaderToScript<\/b> function from my Windows PowerShell ISE profile to add a header to the script file, and also the <b>Add-Help<\/b> function to add comment-based Help to the <b>Copy-ScriptToNewTab<\/b> function. The parameter portion of the script is simple, and it is shown here.<\/p>\n<\/p>\n<p style=\"padding-left: 30px\">Param([switch]$selection)<\/p>\n<\/p>\n<p>Next I check to see if the <i>Selection <\/i>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.<\/p>\n<\/p>\n<p style=\"padding-left: 30px\">if($selection)<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; { $newtab = $psISE.CurrentPowerShellTab.Files.Add()<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; $newtab.Editor.Text = $psise.CurrentFile.Editor.SelectedText }<\/p>\n<\/p>\n<p>If the <i>Selection <\/i>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 <b>Add<\/b><i> <\/i>method from the <b>Files<\/b><i> <\/i>object from the <b>CurrentPowershellTab<\/b><i>. <\/i>If I want only selected text, I use the <b>SelectedText<\/b><i> <\/i>property. If I want the entire script text from the <b>CurrentFileEditor <\/b>object, I use the <b>Text<\/b><i> <\/i>property. This portion of the function is shown here.<\/p>\n<\/p>\n<p style=\"padding-left: 30px\">ELSE<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; { $newtab = $psISE.CurrentPowerShellTab.Files.Add()<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; $newtab.Editor.Text = $psISE.CurrentFile.Editor.text }<\/p>\n<\/p>\n<p>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&mdash;it is a pretty basic function. But I thought I would add it because I am sharing the function. Besides, with the <b>Add-Help<\/b> function, it takes me less than a minute to add comment-based Help. Use of the function is shown in the following image.<\/p>\n<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6560.wes-6-16-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6560.wes-6-16-12-01.png\" alt=\"Image of function\" title=\"Image of function\" \/><\/a><\/p>\n<\/p>\n<p>The complete function is shown here.<\/p>\n<\/p>\n<p style=\"padding-left: 30px\"><b>Copy-ScriptToNewTab<\/b><\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">Function Copy-ScriptToNewTab<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp; &lt;#<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Synopsis<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; This does that<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Description<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; This function does<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Example<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Example-<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Example- accomplishes<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Parameter<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; The parameter<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Notes<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; NAME:&nbsp; Example-<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; AUTHOR: ed wilson, msft<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; LASTEDIT: 06\/10\/2012 10:00:40<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; KEYWORDS:<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; HSG:<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Link<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; Http:\/\/www.ScriptingGuys.com<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;#Requires -Version 2.0<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;#&gt;<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;Param([switch]$selection)<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp; if($selection)<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; { $newtab = $psISE.CurrentPowerShellTab.Files.Add()<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; $newtab.Editor.Text = $psise.CurrentFile.Editor.SelectedText }<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp; ELSE<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; { $newtab = $psISE.CurrentPowerShellTab.Files.Add()<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; $newtab.Editor.Text = $psISE.CurrentFile.Editor.text }<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">} #end function Copy-ScriptToNewTab<\/p>\n<\/p>\n<p>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&rsquo;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, &ldquo;Hi.&rdquo; We hope to see you.<\/p>\n<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,61,45,100],"class_list":["post-9031","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell","tag-windows-powershell-ise"],"acf":[],"blog_post_summary":"<p>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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/9031","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=9031"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/9031\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=9031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=9031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=9031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}