{"id":53833,"date":"2009-05-04T15:18:00","date_gmt":"2009-05-04T15:18:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/05\/04\/hey-scripting-guy-how-can-i-use-windows-powershell-to-handle-mind-numbing-repetitive-tasks\/"},"modified":"2009-05-04T15:18:00","modified_gmt":"2009-05-04T15:18:00","slug":"hey-scripting-guy-how-can-i-use-windows-powershell-to-handle-mind-numbing-repetitive-tasks","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-use-windows-powershell-to-handle-mind-numbing-repetitive-tasks\/","title":{"rendered":"Hey, Scripting Guy! How Can I Use Windows PowerShell to Handle Mind-Numbing, Repetitive Tasks?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> <\/H2>\n<P>Hey, Scripting Guy! I have a number of tasks that I always have to do. These tasks are somewhat repetitive, and I want to be able to write a Windows PowerShell script to eliminate the repetition. Is there something I can use to help with the mind-numbing repetition?<BR><BR>&#8211; CH<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hi CH,<\/P>\n<P>Ed here. Some repetition is good. For example, several years ago the Scripting Wife, a couple of scripting friends, and I went to Aruba. Each day we would walk along the beach at sunset:<\/P><IMG border=\"0\" alt=\"Image of Aruba at sunset\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/may\/hey0504\/hsg-05-04-09-01.jpg\" width=\"500\" height=\"362\"> \n<P>&nbsp;<\/P>\n<P>To be honest, I could do with a bit of that kind of repetition about now. The problem is not the repetition, but the kind of repetition that becomes an issue. Luckily, with Windows PowerShell we can resolve your repetition issue by using the <B>For\u2026Loop<\/B> (or the <B>For<\/B> statement, as it is also known).<\/P>\n<TABLE id=\"EDD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">This week we are looking at scripting Windows PowerShell. Windows PowerShell is installed by default on Windows Server 2008 R2 and Windows 7. It is an optional installation on Windows Server 2008, and a download for <A href=\"http:\/\/www.microsoft.com\/downloads\/details.aspx?displaylang=en&amp;FamilyID=c6ef4735-c7de-46a2-997a-ea58fdfcba63\" target=\"_blank\">Windows Vista<\/A>, <A href=\"http:\/\/www.microsoft.com\/downloads\/details.aspx?displaylang=en&amp;FamilyID=6ccb7e0d-8f1d-4b97-a397-47bcc8ba3806\" target=\"_blank\">Windows XP<\/A>, and <A href=\"http:\/\/www.microsoft.com\/downloads\/details.aspx?displaylang=en&amp;FamilyID=10ee29af-7c3a-4057-8367-c9c1dab6e2bf\" target=\"_blank\">Windows Server 2003<\/A>. The <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\" target=\"_blank\">Windows PowerShell Scripting Hub<\/A> is a good place to start using Windows PowerShell. An excellent book for learning Windows PowerShell is the Microsoft Press book, <A href=\"http:\/\/www.microsoft.com\/MSPress\/books\/authors\/auth10329.aspx\" target=\"_blank\">Microsoft Windows PowerShell Step by Step<\/A>. This book has many hands-on labs and uses real-world examples to show the use of Windows PowerShell.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>In VBScript, a <B>For\u2026Next\u2026Loop<\/B> was somewhat easy to create. An example of a simple <B>For\u2026Next\u2026Loop<\/B> is seen in <B>DemoForLoop.vbs<\/B>. You use the <B>For<\/B> keyword, define a variable to keep track of the count, indicate how far you will go, define your action, and specify the <B>Next<\/B> keyword. That is about all there is to it. It sounds more difficult than it is.<\/P>\n<P><B>DemoForLoop.vbs<\/B><\/P><PRE class=\"codeSample\">For i = 1 To 5\n WScript.Echo i\nNext\n<\/PRE>\n<P>You can achieve the same thing in Windows PowerShell. The structure of the <B>For\u2026Loop<\/B> in Windows PowerShell resembles the structure for VBScript. They both begin with the keyword <B>For<\/B>, they both initialize the variable, and they both specify how far the loop will progress. One thing that is different is that a <B>For\u2026Loop<\/B> in VBScript automatically increments the counter variable. In Windows PowerShell, the variable is not automatically incremented, and we add <B>$i++<\/B> to increment the <B>$i<\/B> variable by one. Inside the script block (which is set off by curly brackets) we display the value of the <B>$i<\/B> variable.<\/P>\n<P><B>DemoForLoop.<\/B><B>ps1<\/B><\/P><PRE class=\"codeSample\">For($i = 0; $i -le 5; $i++)\n{\n &#8216;$i equals &#8216; + $i \n}\n<\/PRE>\n<P>The Windows PowerShell <B>For<\/B> statement is very flexible, and you can leave one or more elements of it out. In the <B>DemoForWithoutInitOrRepeat.ps1<\/B> script, we exclude the first and the last sections of the <B>For<\/B> statement. We set the <B>$i<\/B> variable equal to 0 on the first line of the script. We next come to the <B>For<\/B> statement. In the <B>DemoForLoop.ps1<\/B> script, the <B>$i = 0<\/B> was moved from inside the <B>For<\/B> statement to the first line of the script. The semicolon is still required because it is used to separate the three sections of the statement. The condition portion, <B>$i \u2013le 5<\/B>, is the same as in the previous script. The repeat section, <B>$i ++<\/B>, is not used either. <\/P>\n<P>In the script section of the <B>For<\/B> statement, we display the value of the <B>$i<\/B> variable, and we also increment the value of <B>$i<\/B> by one. There are two kinds of Windows PowerShell strings: expanding and literal. In the <B>DemoForLoop.ps1<\/B> script, you see an example of a literal string\u2014what is entered is what is displayed. This is shown here:<\/P><PRE class=\"codeSample\">&#8216;$i equals &#8216; + $i\n<\/PRE>\n<P>In the <B>DemoForWithoutInitOrRepeat.ps1<\/B> script, you see an example of an expanding string. The value of the variable is displayed\u2014not the variable name itself. To suppress the expanding nature of the expanding string, escape the variable by using the backtick character (<B>`<\/B>). When you use the expanding string in this manner, it enables you to avoid concatenating the string and the variable as we did in the <B>DemoForLoop.ps1<\/B> script. This is shown here:<\/P><PRE class=\"codeSample\">&#8220;`$i is equal to $i&#8221;\n<\/PRE>\n<P>The value of <B>$i<\/B> must be incremented somewhere. Because it was not incremented in the repeat section of the <B>For<\/B> statement, we have to be able to increment it inside the script block. The <B>DemoForWithoutInitOrRepeat.ps1<\/B> script is shown here:<\/P>\n<P><B>DemoForWithoutInitOrRepeat.ps1<\/B><\/P><PRE class=\"codeSample\">$i = 0\nFor(;$i -le 5; )\n{\n &#8220;`$i is equal to $i&#8221;\n $i++\n}\n<\/PRE>\n<P>When we run the <B>DemoForWithoutInitOrRepeat.ps1<\/B> script, the output that is displayed resembles the output produced by the <B>DemoForLoop.ps1<\/B>. You would never be able to tell it was missing two-thirds of the parameters:<\/P><IMG border=\"0\" alt=\"Image of output from DemoForWithoutInitOrRepeat.ps1\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/may\/hey0504\/hsg-05-04-09-02.jpg\" width=\"500\" height=\"158\"> \n<P>&nbsp;<\/P>\n<P>At this point, you may be asking yourself, &#8220;Why would I want to do something such as this?&#8221; Who knows! Suppose that you were walking along the beach in Aruba, and you saw some umbrellas such as these that I photographed:<\/P><IMG border=\"0\" alt=\"Image of umbrellas on a beach in Aruba\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/may\/hey0504\/hsg-05-04-09-03.jpg\" width=\"500\" height=\"375\"> \n<P>&nbsp;<\/P>\n<P>You may ask, &#8220;Why would I want to lounge around on a sandy beach, smear greasy stuff on my face, and get sand in my slippers.&#8221; Or you might say, &#8220;I think I will go take a quiet repose under yonder palm tree.&#8221; Why ask why? Because you can, because it is there, or just because\u2014it really does not matter why. One day, you may find a need for it. The way my scripting goes, if I did not know I could do something, that is the one technique I would need to make a script work correctly. It is always nice to have options. <\/P>\n<P>Speaking of options, you can put your <B>For<\/B> statement into an infinite loop by omitting all three sections of the <B>For<\/B> statement. You must leave the semicolons as position holders. When you omit the three parts of the <B>For<\/B> statement, the <B>For<\/B> statement will resemble the following:<\/P><PRE class=\"codeSample\">for(;;)\n<\/PRE>\n<P>Whereas the <B>ForEndlessLoop.ps1<\/B> script will create an endless loop, you do not have to do this if this is not your desire. You could use an <B>If<\/B> statement to evaluate a condition and to take action when the condition was met. We will look at <B>If<\/B> statements in a later \u201cHey, Scripting Guy!\u201d article. In the <B>ForEndlessLoop.ps1<\/B> script, we display the value of the <B>$i<\/B> variable, and then increment it by one. The semicolon is used to represent a new line. The <B>For<\/B> statement could therefore be written on three lines if you wanted to do this. This would be useful if you had a very complex <B>For<\/B> statement because it would make the code easier to read. The script block for the <B>ForEndlessLoop.ps1<\/B> script could be written on different lines and exclude the semicolon. This is seen here:<\/P><PRE class=\"codeSample\">{\n $i\n $i++\n}\n<\/PRE>\n<P><B>ForEndlessLoop.ps1<\/B><\/P><PRE class=\"codeSample\">for(;;)\n{\n $i ; $i++\n}\n<\/PRE>\n<P>When you run the <B>ForEndlessLoop.ps1<\/B> script, you are greeted with a long line of numbers. To break out of the endless loop, press Ctrl+C inside the Windows PowerShell prompt. The long line of numbers is shown here:<\/P><IMG border=\"0\" alt=\"Image of an endless loop of numbers, which can be ended by pressing CTRL+C\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/may\/hey0504\/hsg-05-04-09-04.jpg\" width=\"500\" height=\"248\"> \n<P>&nbsp;<\/P>\n<P>CH, you can see that working with Windows PowerShell is all about choices: how do you want to work and what are you trying to achieve? The <B>For<\/B> statement in Windows PowerShell is very flexible, and maybe one day, you will find just the problem waiting for the solution that you have. We have to return to work on the Scripting Games. They are coming up shortly, and we need to put the finishing touches on some of the events. We will be making an announcement soon. See you tomorrow. Stay cool.<\/P>\n<P>&nbsp;<\/P>\n<P><B>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/B><\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have a number of tasks that I always have to do. These tasks are somewhat repetitive, and I want to be able to write a Windows PowerShell script to eliminate the repetition. Is there something I can use to help with the mind-numbing repetition?&#8211; CH Hi CH, Ed here. Some repetition [&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":[51,3,4,45],"class_list":["post-53833","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I have a number of tasks that I always have to do. These tasks are somewhat repetitive, and I want to be able to write a Windows PowerShell script to eliminate the repetition. Is there something I can use to help with the mind-numbing repetition?&#8211; CH Hi CH, Ed here. Some repetition [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/53833","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=53833"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/53833\/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=53833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=53833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=53833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}