{"id":7051,"date":"2014-02-01T00:00:21","date_gmt":"2014-02-01T00:00:21","guid":{"rendered":"http:\/\/craftydba.com\/?p=7051"},"modified":"2024-02-17T14:50:43","modified_gmt":"2024-02-17T14:50:43","slug":"executing-powershell-from-the-commandline","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=7051","title":{"rendered":"Executing Powershell From Cmd Line"},"content":{"rendered":"<p>In the last article, we verified that Windows PowerShell was installed on my laptop and determined the version of the scripting engine.  This tallk will be center around how to execute scripts from the command line.<\/p>\n<p>For a simple test case, I created a C:\\TEMP directory.  Inside that directory is a script named &#8220;LIST-ROOT.PS1&#8221; that will change the directory to the root node on the C drive.  After this action, the contents of the root directory is listed for review.<\/p>\n<pre class=\"lang:PS theme:familiar mark:1,2-3\" title=\"list root directory\">\r\n#\r\n# List root directory\r\n#\r\ncd c:\\\r\ndir\r\n<\/pre>\n<p>Lets try executing the script from a command line.  Use the -F option to specify the location of the file.  As we can see below, the command fails since the script is not signed.<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2014\/02\/powershell-cmd-line1.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2014\/02\/powershell-cmd-line1.jpg\" alt=\"powershell-cmd-line1\" width=\"679\" height=\"309\" class=\"aligncenter size-full wp-image-7057\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2014\/02\/powershell-cmd-line1.jpg 679w, https:\/\/craftydba.com\/wp-content\/uploads\/2014\/02\/powershell-cmd-line1-300x136.jpg 300w\" sizes=\"auto, (max-width: 679px) 100vw, 679px\" \/><\/a><\/p>\n<p>For now, we will allow unrestricted access since I am the only person on this laptop.  In a later article, we will talk about signing the script.<\/p>\n<p>The enclosed <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/ee176961.aspx\">MSDN<\/a> link goes over the three possible settings.  <\/p>\n<p>For now, we will use the <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/ee176847.aspx\">Get-ExecutionPolicy<\/a> applet to find out the current state and change the setting with the <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/ee176961.aspx\">Set-ExecutionPolicy<\/a> applet.<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2014\/02\/powershell-cmd-line-2.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2014\/02\/powershell-cmd-line-2.jpg\" alt=\"powershell-cmd-line-2\" width=\"677\" height=\"209\" class=\"aligncenter size-full wp-image-7059\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2014\/02\/powershell-cmd-line-2.jpg 677w, https:\/\/craftydba.com\/wp-content\/uploads\/2014\/02\/powershell-cmd-line-2-300x92.jpg 300w\" sizes=\"auto, (max-width: 677px) 100vw, 677px\" \/><\/a><\/p>\n<p>Let&#8217;s try running this script again with the new execution policy.  Please note that script engine is smart enough to know that the file name is the script to execute.<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2014\/02\/powershell-cmd-line-3.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2014\/02\/powershell-cmd-line-3.jpg\" alt=\"powershell-cmd-line-3\" width=\"677\" height=\"485\" class=\"aligncenter size-full wp-image-7061\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2014\/02\/powershell-cmd-line-3.jpg 677w, https:\/\/craftydba.com\/wp-content\/uploads\/2014\/02\/powershell-cmd-line-3-300x214.jpg 300w\" sizes=\"auto, (max-width: 677px) 100vw, 677px\" \/><\/a><\/p>\n<p>In summary, if you are having issues running a script from the command line, please check the execution policy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the last article, we verified that Windows PowerShell was installed on my laptop and determined the version of the scripting engine. This tallk will be center around how to execute scripts from the command line. For a simple test case, I created a C:\\TEMP directory. Inside that directory is a script named &#8220;LIST-ROOT.PS1&#8221; that will change the directory to the root node on the C drive. After this action, the contents of the root directory is listed for review. # # List root directory # cd c:\\ dir Lets&hellip;<\/p>\n","protected":false},"author":1,"featured_media":6869,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[1039,1038,1040,1041],"class_list":["post-7051","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-other","tag-command-line","tag-developer--powershell--john-f-miner-iii--execute-script","tag-get-executionpolicy","tag-set-executionpolicy"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/7051","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7051"}],"version-history":[{"count":1,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/7051\/revisions"}],"predecessor-version":[{"id":8844,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/7051\/revisions\/8844"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/media\/6869"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}