{"id":352,"date":"2006-12-08T04:00:00","date_gmt":"2006-12-08T12:00:00","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2006\/12\/08\/more-cli-and-powershell\/"},"modified":"2006-12-08T04:00:00","modified_gmt":"2006-12-08T12:00:00","slug":"more-cli-and-powershell","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2006\/12\/08\/more-cli-and-powershell\/","title":{"rendered":"More CLI and PowerShell"},"content":{"rendered":"<p>You don&#8217;t have to choose between the traditional command line interface (CLI) and PowerShell.&nbsp; You can easily run a PowerShell command or even a PowerShell script directly from a command prompt. This means you can integrate some PowerShell functionality into your older batch files. It also means that you can create a scheduled task to execute a PowerShell script!<\/p>\n<p>At a command prompt, run powershell \/? and you&#8217;ll see the proper syntax.&nbsp; Generally, you will want to use the format <span style=\"font-family: Courier New,Courier,mono;\">powershell -command &#8220;&amp;{<span style=\"font-style: italic;\">some powershell expression<\/span>}&#8221;<\/span>.&nbsp; I also prefer to use <span style=\"font-style: italic;\">-nologo<\/span> to suppress the Windows PowerShell logo. This is especially useful if you want to capture the ouput to a text file. This also brings up an important point, because you are calling PowerShell from the CLI, any output returned is <span style=\"color: rgb(51, 51, 255); font-weight: bold;\">text <\/span>and not a PowerShell object. So when you run a CLI command like this:<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">powershell -nologo -command &#8220;&amp; {get-process}&#8221;<\/span><\/p>\n<p>It looks like normal PowerShell output.&nbsp; But if you capture it, it&#8217;s just text. Not that this is a bad thing. It may be just want you want.<\/p>\n<p>I&#8217;ve put together a little batch file demo that you can run. It saves the output from several PowerShell expressions to a text file. It can even run a .ps1 file.<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">@echo off<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">REM PoSHTest.bat<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">set zlog=e:\\temp\\pslog.txt<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">echo %date% %time% &gt; %zlog%<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">powershell -nologo -command &#8220;&amp; {get-wmiobject win32_operatingsystem}&#8221; &gt;&gt;%zlog%<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">REM you can use aliases<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">powershell -nologo -command &#8220;&amp; {gwmi win32_computersystem}&#8221; &gt;&gt;%zlog%<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">REM You can use filtering blocks<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">powershell -nologo -command &#8220;&amp; {get-service | where {$_.status -eq &#8216;running&#8217;}}&#8221; &gt;&gt;%zlog%<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">REM You can call other PowerShell scripts<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">powershell -nologo -command &#8220;&amp; {s:\\posh\\scheduleTest.ps1}&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">echo See %zlog% for results<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">set zlog=<\/span><\/p>\n<p>If you want to try this out be sure to change the zlog variable path and any PowerShell script you might want to run. You should test this first interactively. Assuming everything works, then try it out as a scheduled task on your system.&nbsp; You should have an appropriate log file after the job completes.<\/p>\n<p>If you try to schedule a batch file to run a PowerShell script, make sure the ps1 script doesn&#8217;t require any user input or interaction. Unless, of course, you create the job to be interactive.<\/p>\n<p>Once you get the hang of it, you&#8217;ll realize you don&#8217;t always have to launch a full PowerShell instance to get something done. If you need the results of a simple PowerShell expression, you can stay in the CLI and get the best of both worlds.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You don&#8217;t have to choose between the traditional command line interface (CLI) and PowerShell.  You can easily run a PowerShell command or even a PowerShell script directly from a command prompt. This means you can integrate some PowerShell functionality into your older batch files. It also means that you can create a scheduled task to execute a PowerShell script!<\/p>\n","protected":false},"author":3,"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":[18,2,25],"tags":[],"class_list":["post-352","post","type-post","status-publish","format-standard","hentry","category-batchcmd","category-general","category-windows-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/352","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\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=352"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/352\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}