{"id":104,"date":"2019-07-06T20:20:28","date_gmt":"2019-07-06T19:20:28","guid":{"rendered":"http:\/\/192.168.8.14\/?p=104"},"modified":"2019-07-06T21:20:27","modified_gmt":"2019-07-06T20:20:27","slug":"powershell-dot-source","status":"publish","type":"post","link":"https:\/\/www.jasonstreet.com\/?p=104","title":{"rendered":"Powershell Dot-Source"},"content":{"rendered":"\n<p>Using Dot Sourcing is a quick and easy way of getting functions or data into your Powershell console or script.<\/p>\n\n\n\n<p>If you run a Powershell script from a console, all the data in memory is deleted when the script ends  (unlike the ISE where it is still in memory).<br>This is because the script is running in a different session (memory space) so is separate from the Powershell console that runs the script.<\/p>\n\n\n\n<p>This quick script will demonstrate this.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$one = 1\n$two = 2\n$three = 3\n$bob = $one + $three\n$jase\n<\/pre><\/div>\n\n\n<p>If I run this script in the ISE I will get (surprisingly enough) no output.<br>but I am able to see the variables $one, $two, $three and $bob<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"274\" height=\"256\" src=\"http:\/\/192.168.8.14\/wp-content\/uploads\/2019\/07\/Capture001.png\" alt=\"\" class=\"wp-image-105\"\/><\/figure>\n\n\n\n<p><br><br>Now if I try the same thing in a standard Powershell window I get.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"514\" height=\"85\" src=\"http:\/\/192.168.8.14\/wp-content\/uploads\/2019\/07\/Capture002.png\" alt=\"\" class=\"wp-image-106\" srcset=\"https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/07\/Capture002.png 514w, https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/07\/Capture002-300x50.png 300w\" sizes=\"auto, (max-width: 514px) 100vw, 514px\" \/><\/figure>\n\n\n\n<p>The variables in the script test.ps1 are not accessible in the console once the script has finished executing. Effectively the test.ps1 script is running in a separate part of memory (or a session) that is cleared down when exiting.<\/p>\n\n\n\n<p>So now we Dot Source the script.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"596\" height=\"109\" src=\"http:\/\/192.168.8.14\/wp-content\/uploads\/2019\/07\/Capture003.png\" alt=\"\" class=\"wp-image-107\" srcset=\"https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/07\/Capture003.png 596w, https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/07\/Capture003-300x55.png 300w\" sizes=\"auto, (max-width: 596px) 100vw, 596px\" \/><\/figure>\n\n\n\n<p>By adding a full stop in front of the script we instruct Powershell to run in the current session. This makes all the variables available after execution.<\/p>\n\n\n\n<p>It also works the other way in that any data in memory is available to the script when it runs.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"556\" height=\"65\" src=\"http:\/\/192.168.8.14\/wp-content\/uploads\/2019\/07\/Capture004.png\" alt=\"\" class=\"wp-image-108\" srcset=\"https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/07\/Capture004.png 556w, https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/07\/Capture004-300x35.png 300w\" sizes=\"auto, (max-width: 556px) 100vw, 556px\" \/><\/figure>\n\n\n\n<p>So if you don&#8217;t want to write a module but you do want some code available in your Powershell console (session) just Dot Source it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using Dot Sourcing is a quick and easy way of getting functions or data into your Powershell console or script. If you run a Powershell script from a console, all the data in memory is deleted when the script ends (unlike the ISE where it is still in memory).This is because the script is running&#8230;<\/p>\n","protected":false},"author":1,"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":[5,4],"tags":[21,23,13,22],"class_list":["post-104","post","type-post","status-publish","format-standard","hentry","category-powercli","category-powershell","tag-dot-source","tag-learning","tag-powershell","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts\/104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=104"}],"version-history":[{"count":0,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts\/104\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}