{"id":8044,"date":"2014-10-28T06:00:00","date_gmt":"2014-10-28T13:00:00","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/?p=8044"},"modified":"2015-07-07T08:49:05","modified_gmt":"2015-07-07T15:49:05","slug":"run-selection-v-run-selection-in-console-powershell-studio-2014","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2014\/10\/28\/run-selection-v-run-selection-in-console-powershell-studio-2014\/","title":{"rendered":"Run Selection v. Run Selection in Console"},"content":{"rendered":"<p>PowerShell Studio is full of features and options. As a new user, I\u2019m trying to learn as many as I can. There are four options in the <strong>Run<\/strong> menu, so, of course, I tried them all. But the result was unexpected.<\/p>\n<p>&nbsp;<\/p>\n<blockquote><p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/10\/image12.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/10\/image_thumb10.png\" alt=\"image\" width=\"280\" height=\"173\" border=\"0\" \/><\/a><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p><strong>Run<\/strong> and <strong>Run Selection<\/strong> run your code and show the result in the Output pane. <strong>Run in Console<\/strong> and <strong>Run Selection in Console<\/strong> run your code and show the result in the Console pane, which is a separate Windows PowerShell process that PowerShell Studio hosts. That\u2019s expected.<\/p>\n<p>&nbsp;<\/p>\n<p>But <strong>Run Selection<\/strong> and <strong>Run Selection in Console <\/strong>are very different from each other.<\/p>\n<ul>\n<li><strong>Run Selection<\/strong> runs each selection in a separate Windows PowerShell session. It\u2019s as though you opened a PowerShell window, ran some commands, displayed the results, and immediately closed the window. If you use <strong>Run Selection<\/strong> to run a few more commands, you just opened a new window with a new session that is unaware of the previous session.<\/li>\n<li><strong>Run Selection in Console <\/strong>uses the same Windows PowerShell session for all commands. You might have heard this called a \u201cpersistent runspace.\u201d When you run a series of <strong>Run Selection in Console<\/strong> commands, all of the commands run in the same session and they\u2019re aware of each other. If you create a session-specific object, such as a variable, it\u2019s available to all selections that you run.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>To test the difference, run the following commands as separate selections, first by using <strong>Run Selection<\/strong> and then by using <strong>Run Selection in Console<\/strong>.<\/p>\n<ul>\n<li><span style=\"font-family: Lucida Console; font-size: small;\">$a = &#8220;Try the zip cmdlets (-Archive) in Windows PowerShell 5.0 Preview&#8221; <\/span><\/li>\n<li><span style=\"font-family: Lucida Console; font-size: small;\">$a<\/span><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>If you use <strong>Run Selection<\/strong>, the result of the final &#8220;<span style=\"font-family: Lucida Console; font-size: small;\">$a<\/span>&#8221; statement is $null, because the assignment statement ran in a different session.<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/10\/image13.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/10\/image_thumb11.png\" alt=\"image\" width=\"339\" height=\"145\" border=\"0\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>But if you use <strong>Run Selection in Console<\/strong>, the result is:<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/10\/image14.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/10\/image_thumb12.png\" alt=\"image\" width=\"491\" height=\"172\" border=\"0\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>If you need a new Windows PowerShell session in the Console pane, type <strong>exit<\/strong> or right-click and click <strong>Restart Shell<\/strong>. (If you type <strong>powershell<\/strong>, you get a nested session, just like you would in the Windows PowerShell console. To return to the original session, type <strong>quit<\/strong>.)<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/10\/image15.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/10\/image_thumb13.png\" alt=\"image\" width=\"386\" height=\"192\" border=\"0\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Now that I understand these features, I can use PowerShell Studio more adeptly. And, what about those Archive cmdlets? I\u2019ll save that treat for another post!<\/p>\n<p><i>June Blender is a technology evangelist at SAPIEN Technologies, Inc. You can reach her at <a href=\"mailto:juneb@sapien.com\">juneb@sapien.com<\/a> and follow her on Twitter at <a href=\"https:\/\/twitter.com\/juneb_get_help\">@juneb_get_help<\/a>.<\/i><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PowerShell Studio is full of features and options. As a new user, I\u2019m trying to learn as many as I can. There are four options in the Run menu, so, of course, I tried them all. But the result was unexpected. &nbsp; &nbsp; Run and Run Selection run your code and show the result in [&hellip;]<\/p>\n","protected":false},"author":31,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[932,703,303],"tags":[934,874,930],"class_list":["post-8044","post","type-post","status-publish","format-standard","hentry","category-beginners","category-powershell-studio","category-tip","tag-juneb","tag-powershell-studio-2014","tag-ui"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/8044","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=8044"}],"version-history":[{"count":3,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/8044\/revisions"}],"predecessor-version":[{"id":9497,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/8044\/revisions\/9497"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=8044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=8044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=8044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}