{"id":66393,"date":"2006-09-26T18:27:00","date_gmt":"2006-09-26T18:27:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/09\/26\/how-can-i-run-a-windows-powershell-script-from-the-run-dialog-box-or-a-shortcut\/"},"modified":"2006-09-26T18:27:00","modified_gmt":"2006-09-26T18:27:00","slug":"how-can-i-run-a-windows-powershell-script-from-the-run-dialog-box-or-a-shortcut","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-run-a-windows-powershell-script-from-the-run-dialog-box-or-a-shortcut\/","title":{"rendered":"How Can I Run a Windows PowerShell Script from the Run Dialog Box or a Shortcut?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I run a Windows PowerShell script from the <B>Run<\/B> dialog box or from a shortcut? In the <B>Run<\/B> dialog box I type something like <B>c:\\scripts\\test.ps1<\/B> but my script just opens up in Notepad.<BR><BR>&#8212; RS<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, RS. We have just one thing to say: University of Washington 29, UCLA 19. And this after the Huskies trailed 16-0 late in the first half. Granted, it&#8217;s still early, but at 3-1 the UW already has as many wins this year as they&#8217;ve had in the last two seasons combined. <\/P>\n<P>But that\u2019s all we\u2019re going to say. After all, the Scripting Guy who writes this column knows enough about sports to know better than to jinx a team just when things are starting to look up. So let&#8217;s just say that it&#8217;s always nice to beat UCLA and leave it at that. See you all tomorrow!<\/P>\n<P>Pardon? How can you run a Windows PowerShell script from the <B>Run<\/B> dialog box or from a shortcut? Um, didn&#8217;t we tell you that we only had one thing to say? And we already said it: University of Washington 29, UCLA 19. <\/P>\n<P>Could we put you on hold for a second? Really? You\u2019re kidding, right? Wow; we wouldn\u2019t have guessed <I>that<\/I> in a million years. <\/P>\n<P>OK. Apparently, or at least from what we\u2019ve been told, not everyone in the world is a college football fan. That\u2019s hard for us to believe: people would rather know how to run a Windows PowerShell script from the <B>Run<\/B> dialog box or from a shortcut than talk about Saturday\u2019s Husky-UW game? But, just in case, we\u2019ll see if we can answer your question, RS.<\/P>\n<P>No, no need to thank us; it\u2019s no problem. Besides, we have a few hours to kill before the Husky Highlight show comes on anyway.<\/P>\n<P>So why <I>do<\/I> your Windows PowerShell scripts keep opening up in Notepad? That\u2019s because the Windows shell \u2013 bless its heart \u2013 doesn\u2019t know what to do with Windows PowerShell scripts. The last time we installed Windows PowerShell (and remember, the application is still in beta, so the installation process might have changed since then) script files (files with a .ps1 file extension) were automatically associated with Notepad. That\u2019s probably why your script files keep opening up in Notepad: ours do, too, simply because that\u2019s what the Windows shell <I>does<\/I> with .ps1 files.<\/P>\n<P>Of course, that might lead you to believe that all you have to do is associate .ps1 files with the Windows PowerShell executable file and everything will be hunky-dory. That\u2019s what we thought, too, but that doesn\u2019t seem to work. Instead, in order to start a Windows PowerShell script from the <B>Run<\/B> dialog box or from a shortcut (or, for that matter, from Cmd.exe) you need to explicitly start Windows PowerShell and then pass the path to the script file as a startup parameter. In other words, <I>this<\/I> command will start Windows PowerShell and run the script C:\\Scripts\\Test.ps1:<\/P><PRE class=\"codeSample\">powershell.exe c:\\scripts\\test.ps1\n<\/PRE>\n<P>Of course, if you immediately ran off and tried that command, without bothering to read the rest of the column (for shame!) you were likely disappointed. \u201cWait a second; that doesn\u2019t work, either,\u201d you\u2019re likely grumbling. \u201cI saw a brief flash and then, nothing. Nice try, Scripting Guys, but no cigar!\u201d <\/P>\n<P>To tell you the truth, your command probably <I>did<\/I> work; the problem is that Windows PowerShell started, the script ran, the script finished, and then Windows PowerShell automatically terminated, just like that. You can verify that this is what\u2019s happening by running a script that saves data to a text file. Start the script using the method outlined above, and then check to see if the text file was created and the data saved. If it was, then the command worked; it\u2019s just that Windows PowerShell did its thing and then, like the Lone Ranger, disappeared before you could even thank it.<\/P>\n<P>Now, that\u2019s all fine and dandy \u2026 unless you need to read the output onscreen. In that case we have a problem, because everything typically happens way too fast for the human eye to follow. (Granted, the Scripting Editor insists that she can spot dangling participles and split infinitives even at that speed. But, remember, we said it was too fast for the <I>human<\/I> eye to follow.) If you need to read output onscreen what you have to do is tell Windows PowerShell to keep its command window open after the script finishes running. That\u2019s something you can do by adding the <B>\u2013noexit<\/B> parameter to your command:<\/P><PRE class=\"codeSample\">powershell.exe \u2013noexit c:\\scripts\\test.ps1\n<\/PRE>\n<P>Give that a try and see what happens. When you do, make sure you leave \u2013noexit as the first parameter. In our experience, switching the parameters around (that is, first specifying the script path and <I>then<\/I> tacking on \u2013noexit) won\u2019t work.<\/P>\n<P>But, then again, you should always do <I>everything<\/I> exactly the way the Scripting Guys tell you. <\/P>\n<P>OK, good point, even though the first explosion wasn\u2019t <I>entirely<\/I> our fault; if dolphins are so smart then they should known better. But at least type your Windows PowerShell command exactly the way we told you.<\/P>\n<P>We hope that solves your problem, RS. If you need more information about Windows PowerShell you might take a look at our <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\"><B>Windows PowerShell<\/B><\/A> center; among other things, you\u2019ll find a <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/msh\/payette1.mspx\"><B>new excerpt<\/B><\/A> from the forthcoming book <I>Windows PowerShell in Action<\/I>, written by Microsoft\u2019s own Bruce Payette. And stay tuned for Windows PowerShell Week, a series of five webcasts (aimed squarely at beginners) scheduled for November 6-10. We\u2019ll have more information on Windows PowerShell Week very soon.<\/P>\n<P>Most important of all, next week the Huskies travel to Arizona to play the Wildcats. Could the Huskies really start the season 4-1, including 2-0 in conference play? Let\u2019s put it this way: if they do, it\u2019s a pretty safe bet you\u2019ll be able to read about it here. Will you also be able to read about scripting-related stuff here? In the immortal words of Bart Simpson, we can\u2019t promise to try. But we promise to try to try.<\/P><BR>\n<DIV>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"\"><A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/sept06\/hey0926.mspx#top\"><IMG height=\"9\" alt=\"Top of page\" src=\"http:\/\/www.microsoft.com\/technet\/mnplibrary\/templates\/MNP2.Common\/images\/arrow_px_up.gif\" width=\"7\" border=\"0\"><\/A><A class=\"topOfPage\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/sept06\/hey0926.mspx#top\">Top of page<\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I run a Windows PowerShell script from the Run dialog box or from a shortcut? In the Run dialog box I type something like c:\\scripts\\test.ps1 but my script just opens up in Notepad.&#8212; RS Hey, RS. We have just one thing to say: University of Washington 29, UCLA 19. And [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[2,3,4,45],"class_list":["post-66393","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-running","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I run a Windows PowerShell script from the Run dialog box or from a shortcut? In the Run dialog box I type something like c:\\scripts\\test.ps1 but my script just opens up in Notepad.&#8212; RS Hey, RS. We have just one thing to say: University of Washington 29, UCLA 19. And [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66393","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=66393"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66393\/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=66393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}