{"id":639,"date":"2014-09-23T00:01:00","date_gmt":"2014-09-23T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/09\/23\/fun-formatting-onespart-2-the-method\/"},"modified":"2014-09-23T00:01:00","modified_gmt":"2014-09-23T00:01:00","slug":"fun-formatting-onespart-2-the-method","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/fun-formatting-onespart-2-the-method\/","title":{"rendered":"Fun Formatting Ones&#8212;Part 2: The Method"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: June Blender explains Doug Finke&#8217;s multiplication and formatting trick. Today&#8230;the method.<\/span>\nMicrosoft Scripting Guy, Ed Wilson, is here. This is the second part of a two-part series written by June Blender, Honorary Scripting Guy. In the first part, <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2014\/09\/22\/fun-formatting-ones-part-1-the-task.aspx\">Fun Formatting Ones&mdash;Part 1: The Task<\/a>, we discussed the task of printing Doug&#8217;s multiplication table. In this part, we&#8217;ll talk about the techniques that he used, including formatting the table.<\/p>\n<p style=\"margin-left:30px\"><b>Note<\/b>&nbsp;If you already understand an element of the method, such as Ranges, you can skip that section. The sections are independent.\nHere&#8217;s June&#8230;\nToday, I&#8217;ll explain the techniques that Doug used to solve the task, including an interesting loop and string formatting.<\/p>\n<h2>Recap of Part 1<\/h2>\n<p>Let&#8217;s recap what we learned in Part 1.\nDoug wanted to create this table of products in Windows PowerShell.<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1 * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 = 1<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 11 * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;11 = 121<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp; 111 * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;111 = 12321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; 1111 * &nbsp;&nbsp;&nbsp;&nbsp;1111 = 1234321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">&nbsp;&nbsp; 11111 * &nbsp;&nbsp;&nbsp;11111 = 123454321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">&nbsp; 111111 * &nbsp;&nbsp;111111 = 12345654321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">&nbsp;1111111 * &nbsp;1111111 = 1234567654321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">11111111 * 11111111 = 123456787654321\nEach row of the table multiplies <i>n<\/i> digits of 1s and shows the product. The number of 1-digits increases with each row. To do this, he creates a loop that goes from 1 to 8 and prints that number of 1s and their product. For each row:<\/p>\n<ul>\n<li>Print <i>n <\/i>digits of &#8220;1&#8221;&nbsp;&nbsp; (&#8220;1&#8221; * $i)<\/li>\n<li>An &#8221; x &#8220;<\/li>\n<li>Repeat <i>n<\/i> digits of &#8220;1&#8221;<\/li>\n<li>An &#8221; = &#8220;<\/li>\n<li>The product of the <i>n<\/i> digits, as though they were numbers<\/li>\n<\/ul>\n<p>To get the product of a number string, use the <b>Invoke-Expression<\/b> cmdlet. It evaluates the string as an arithmetic equation, for example:<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">PS C:&gt; &#8220;1111 * 1111&#8221;<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">&#8220;1111 * 1111&#8221;<\/p>\n<p class=\"Code\">&nbsp;<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">PS C:&gt; Invoke-Expression -command &#8220;1111 * 1111&#8221;<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">1234321\nDoug uses a more succinct form of the same <b>Invoke-Expression<\/b> command:<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">&#8220;1111 * 1111&#8221; | iex\nSo the Windows PowerShell statement that creates the string for each row is:<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">&#8220;1&#8221; * $i + &#8221; x &#8221; + &#8220;1&#8221; * $i + &#8221; = &#8221; = (&#8220;1&#8221; * $i) * (&#8220;1&#8221; * $i) | iex\nWe assign &#8220;1&#8221; * $i to the variable <b>$n<\/b> to make this a bit cleaner:<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">$n = &#8220;1&#8221; * $i<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">$n + &#8221; x &#8221; + $n + &#8221; = &#8221; + (&#8220;$n * $n&#8221; | iex)<\/p>\n<p style=\"margin-left:30px\">In a loop from 1 to 8 (inclusive):<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">for ($i = 1; $i -le 8; $i++)<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">{<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">$n = &#8220;1&#8221; * $i<\/p>\n<p class=\"Code\">&nbsp;<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">$n + &#8221; x &#8221; + $n + &#8221; = &#8221; + (&#8220;$n * $n&#8221; | iex)<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">}<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">1 x 1 = 1<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">11 x 11 = 121<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">111 x 111 = 12321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">1111 x 1111 = 1234321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">11111 x 11111 = 123454321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">111111 x 111111 = 12345654321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">1111111 x 1111111 = 1234567654321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">11111111 x 11111111 = 123456787654321\nThat&#8217;s really close. But in his post, Doug uses a different type of loop and string formatting. Let&#8217;s discuss those.<\/p>\n<h2>Ranges<\/h2>\n<p>Instead of using a typical <b>For<\/b> loop, Doug pipes a range of numbers, 1-8, to the <b>ForEach-Object<\/b> cmdlet.<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">1..8 | ForEach-Object {<span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n<p class=\"Code\" style=\"margin-left:30px\">$n = &#8220;1&#8221; * $i; $n + &#8221; x &#8221; + $n + &#8221; = &#8221; + (&#8220;$n * $n&#8221; | iex)<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">}\nA <i>range<\/i> is an array of numbers that starts at a beginning number, stops at (and includes) an ending number, and includes all numbers in between in numeric order. For example, a range of numbers from 1 to 5 is:&nbsp; 1, 2, 3, 4, 5. The syntax for a range is:&nbsp;<\/p>\n<p style=\"margin-left:30px\">$start<b>..<\/b>$stop\nThere are no intervening spaces. You can use negative and positive numbers, and you can create ranges that count down or up, for example:<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">PS C:&gt; 0..6<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">0<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">1<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">2<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">3<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">4<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">5<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">6<\/p>\n<p class=\"Code\">&nbsp;<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">PS C:&gt; -4..4<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">-4<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">-3<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">-2<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">-1<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">0<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">1<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">2<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">3<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">4<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">\n<p class=\"Code\" style=\"margin-left:30px\">PS C:&gt; 8..-3<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">8<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">7<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">6<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">5<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">4<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">3<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">2<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">1<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">0<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">-1<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">-2<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">-3\nBest of all, the starting and the stopping numbers can be variables or expressions that can change over time.<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">PS C:&gt; $start = 0<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">PS C:&gt; $stop = 5<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">PS C:&gt; $start..$stop<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">0<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">1<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">2<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">3<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">4<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">5<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">PS C:&gt; $start = 6<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">PS C:&gt; $start++..$stop&#8211;<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">7<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">6<\/p>\n<h2>Piping the items in the range<\/h2>\n<p>Doug creates a range of numbers from 1 to 8 (1..8) and then pipes the range to the <b>ForEach-Object <\/b>cmdlet.\nWhen you send it a collection of objects (any kind), the pipeline operator ( <b>|<\/b> ) takes each item from the collection (one at a time) and sends it to the next command:<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">1..8 | &lt;next thing&gt;\nIs equivalent to:<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">1, 2, 3, 4, 5, 6, 7, 8 | &lt;next thing&gt;\nOr:<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">1| &lt;next thing&gt;<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">2| &lt;next thing&gt;<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">&hellip;<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">8| &lt;next thing&gt;\nIn the pipeline command, the current object coming down the pipeline is represented by good old <b>$_<\/b>, for example:<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">PS C:&gt; 1..8 | ForEach-Object {&#8220;I need $_ new servers.&#8221;}<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">I need 1 new servers.<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">I need 2 new servers.<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">I need 3 new servers.<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">I need 4 new servers.<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">I need 5 new servers.<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">I need 6 new servers.<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">I need 7 new servers.<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">I need 8 new servers.\nYou can use a range controlled <b>ForEach-Object<\/b> command in place of any <b>For<\/b> loop. Here&#8217;s the equivalent <b>For<\/b> loop. The <b>$i<\/b> variable starts at 1, continues while <b>$i<\/b> is less-than or equal-to 8 (because 8 is included in the range), and it increments <b>$i<\/b> on each pass. In this case, the value that is increasing is <b>$i<\/b>, not <b>$_<\/b>:<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">PS C:&gt; For ($i = 1; $i -le 8; $i++) {&#8220;I need $i new servers.&#8221;}<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">I need 1 new servers.<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">I need 2 new servers.<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">I need 3 new servers.<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">I need 4 new servers.<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">I need 5 new servers.<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">I need 6 new servers.<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">I need 7 new servers.<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">I need 8 new servers.\nLet&#8217;s replace the <b>For<\/b> loop that creates Doug&#8217;s 1s table with a statement that pipes a range to a <b>ForEach-Object <\/b>command. Here&#8217;s the <b>For<\/b> loop:<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">for ($i = 1; $i -le 8; $i++)<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">{<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; $n = &#8220;1&#8221; * $i<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; $n + &#8221; x &#8221; + $n&nbsp; + &#8221; = &#8221; + (&#8220;$n * $n&#8221; | iex)<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">}\nHere&#8217;s the new range controlled <b>ForEach<\/b> loop. The <b>$_ <\/b>takes the place of <b>$i<\/b> in the <b>For<\/b> loop:<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">1..8 | ForEach {<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; $n = &#8220;1&#8221; * $_<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; $n + &#8221; x &#8221; + $n&nbsp; + &#8221; = &#8221; + (&#8220;$n * $n&#8221; | iex)<\/p>\n<p style=\"margin-left:30px\">}\nDoug makes it a one-liner. In a one-liner, we don&#8217;t have each command on a separate line, so we add a statement terminator (<b> ;<\/b> ) to end the assignment statement. The remainder of the command is a single statement:<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">1..8 | ForEach {$n = &#8220;1&#8221; * $_; $n + &#8221; x &#8221; + $n&nbsp; + &#8221; = &#8221; + (&#8220;$n * $n&#8221; | iex)}<\/p>\n<p class=\"Code\">&nbsp;<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">1 x 1 = 1<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">11 x 11 = 121<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">111 x 111 = 12321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">1111 x 1111 = 1234321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">11111 x 11111 = 123454321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">111111 x 111111 = 12345654321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">1111111 x 1111111 = 1234567654321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">11111111 x 11111111 = 123456787654321\nHanging in there? Now, let&#8217;s add some formatting.<\/p>\n<h2>Formatting strings with -f<\/h2>\n<p>Windows PowerShell uses the formatting statements in the .NET library that are designed for strings. They&#8217;re really powerful, so it&#8217;s worth learning how to use them.\nThe primary MSDN doc about this topic starts with <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/txafckwd(v=vs.110).aspx\" target=\"_blank\">Composite Formatting<\/a>, a really well-written topic that is worth reading and bookmarking. There are lots of blog posts about string formatting in Windows PowerShell, including one by Ed Wilson (<a href=\"http:\/\/blogs.technet.comhttps:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-format-strings-with-composite-formatting\/\" target=\"_blank\">Use PowerShell to Format Strings with Composite Formatting<\/a>) and one by me (<a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2014\/02\/15\/string-formatting-in-windows-powershell.aspx\" target=\"_blank\">String Formatting in Windows PowerShell<\/a>).\nString formatting is powerful and complex, but we can start with the basics. A formatted string statement has the following form:<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">&#8220;{0} and {1}&#8221; -f &lt;expressionA&gt;, &lt;expressionB&gt;\nThe <b>-f<\/b> (for <b>Format<\/b>) divides the statement into two parts.\nThe double-quoted string to the left of the <b>-f <\/b>includes integers that are placeholders for strings. The placeholder integers must begin with 0 and increase by 1. They&#8217;re enclosed in curly braces, for example:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {0}\nTo the right of the <b>-f<\/b> is a comma-separated array of one or more expressions. The result of each expression takes the place of a placeholder. The first expression takes the place of <b>{0}<\/b> in the string; the second expression takes the place of <b>{1}<\/b>, and so on, for example:<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">PS C:&gt; &#8220;{0} is {1}.&#8221; -f &#8220;PowerShell&#8221;, &#8220;fun&#8221;<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">PowerShell is fun.<span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n<p class=\"Code\" style=\"margin-left:30px\"><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n<p class=\"Code\" style=\"margin-left:30px\">PS C:&gt;&#8221;{0} is {1}&#8221; -f (&#8220;car&#8221; * 3), &#8220;redun&#8221; + &#8220;dant&#8221;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">carcarcar is redundant.<\/p>\n<p class=\"Code\"><span style=\"font-size:12px\">The placeholders can be repeated in the string, but you need to have exactly one expression for each placeholder.<\/span><\/p>\n<p class=\"Code\" style=\"margin-left:30px\">PS C:&gt;&#8221;How much {0} could a {0}{1} {1} if a {0}{1} could {1} {0}?&#8221; -f &#8220;wood&#8221;, &#8220;chuck&#8221;<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">How much wood could a woodchuck chuck if a woodchuck could chuck wood?\nLet&#8217;s see how Doug used formatting. He started with a string of the form:&nbsp;<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">ones x ones = product<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">$n x $n = (&#8220;$n * $n&#8221; | iex)\nHe replaced the ones with <b>&#8220;{0}&#8221;<\/b> and product with <b>&#8220;{1}&#8221;<\/b>. Note that this is a single string. We don&#8217;t need to use string addition to concatenate the <b>x<\/b> and the<b> =<\/b>. They&#8217;re just part of the string:<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">&#8220;{0} x {0} = {1}&#8221;\nThen he used the <b>-f<\/b> statement to replace every <b>{0}<\/b> with <b>$n<\/b> and every <b>{1}<\/b> with <b>(&#8220;$n * $n | iex&#8221;)<\/b>:<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">&#8220;{0} x {0} = {1}&#8221; -f $n, (&#8220;$n * $n | iex&#8221;)\nSo, the original statement:<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">1..8 | ForEach {$n = &#8220;1&#8221; * $_; $n + &#8221; x &#8221; + $n&nbsp; + &#8221; = &#8221; + (&#8220;$n * $n&#8221; | iex)}\nBecomes:<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">1..8 | ForEach {$n = &#8220;1&#8221; * $_; &#8220;{0} x {0} = {1}&#8221; -f $n, (&#8220;$n * $n&#8221; | iex)}<br \/> The result is the same:<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">1 x 1 = 1<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">11 x 11 = 121<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">111 x 111 = 12321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">1111 x 1111 = 1234321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">11111 x 11111 = 123454321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">111111 x 111111 = 12345654321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">1111111 x 1111111 = 1234567654321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">11111111 x 11111111 = 123456787654321\nThe last step is aligning it so it looks even cooler.<\/p>\n<h2>Alignment in formatted strings<\/h2>\n<p>Among the many things you can do with string formatting is to align the characters in a string. To place characters in a string of a certain size, add an alignment value to the placeholder. Positive values align to the right, and negative values align to the left. The syntax is:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&lt;placeholder&gt;,&lt;alignment&gt;}\nFor example, this statement declares placeholder <b>{0}<\/b> and places the result in a 5-character string where the <b>&#8220;a&#8221;<\/b> character is in position 5:<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">PS C:&gt; &#8220;{0, 5}&#8221; -f &#8220;a&#8221;<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; a\nThis statement uses a negative value to left-align the <b>&#8220;a&#8221;<\/b> in a 5-character space. I&#8217;ve put an <b>&#8220;x&#8221;<\/b> in the next position, so you can see the alignment more clearly:<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">PS C:ps-test&gt; &#8220;{0, -5}{1}&#8221; -f &#8220;a&#8221;, &#8220;x&#8221;<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">a&nbsp;&nbsp;&nbsp; x\nIn his &#8220;Fun with Ones&#8221; post, Doug uses an alignment value of 9 (+9) to right-align the <b>$n<\/b> strings in a 9-character field. The alignment changes this:<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">1..8 | ForEach {$n = &#8220;1&#8221; * $_; &#8220;{0} x {0} = {1}&#8221; -f $n, (&#8220;$n * $n&#8221; | iex)}<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">1 x 1 = 1<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">11 x 11 = 121<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">111 x 111 = 12321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">1111 x 1111 = 1234321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">11111 x 11111 = 123454321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">111111 x 111111 = 12345654321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">1111111 x 1111111 = 1234567654321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">11111111 x 11111111 = 123456787654321\nTo this:<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">1..8 | ForEach {$n = &#8220;1&#8221; * $_; &#8220;{0, 9} x {0, 9} = {1}&#8221; -f $n, (&#8220;$n * $n&#8221; | iex)}<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1 x&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1 = 1<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 11 x&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 11 = 121<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 111 x&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 111 = 12321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp; 1111 x&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1111 = 1234321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; 11111 x&nbsp;&nbsp;&nbsp;&nbsp; 11111 = 123454321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">&nbsp;&nbsp; 111111 x&nbsp;&nbsp;&nbsp; 111111 = 12345654321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">&nbsp; 1111111 x&nbsp;&nbsp; 1111111 = 1234567654321<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">&nbsp;11111111 x&nbsp; 11111111 = 123456787654321\nSo, what did we learn from taking some time to interpret Doug&#8217;s work? Here we go&#8230;<\/p>\n<ul>\n<li><strong>Ranges<\/strong><\/li>\n<\/ul>\n<p class=\"Code\" style=\"margin-left:60px\">PS C:&gt;1..8<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">1<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">2<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">3<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">4<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">5<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">6<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">7<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">8<\/p>\n<ul>\n<li><strong>Replacing a For loop with a range piped to a ForEach-Object command<\/strong><\/li>\n<\/ul>\n<p class=\"Code\" style=\"margin-left:60px\">for ($i = 1; $i -le 8; $i++) {&#8220;1&#8221; * $i}<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">1<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">11<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">111<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">1111<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">11111<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">111111<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">1111111<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">11111111<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">1..8 | ForEach-Object {&#8220;1&#8221; * $_}<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">1<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">11<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">111<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">1111<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">11111<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">111111<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">1111111<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">11111111<\/p>\n<ul>\n<li><strong>Multiplying strings<\/strong><\/li>\n<\/ul>\n<p class=\"Code\" style=\"margin-left:60px\">PS C&gt;&#8221;1&#8243; * 3<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">111<\/p>\n<ul>\n<li><strong>Adding strings<\/strong><\/li>\n<\/ul>\n<p class=\"Code\" style=\"margin-left:60px\">PS C:&gt; (&#8220;1&#8243; * 3) + &#8221; x &#8221; + (&#8220;1&#8243; * 3) + &#8221; = &#8220;<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">111 x 111 =<\/p>\n<ul>\n<li><strong>Using Invoke-Expression to evaluate number strings<\/strong><\/li>\n<\/ul>\n<p class=\"Code\" style=\"margin-left:60px\">PS C:ps-test&gt; &#8220;111 * 111&#8221;<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">111 * 111<\/p>\n<p class=\"Code\" style=\"margin-left:30px\">\n<p class=\"Code\" style=\"margin-left:60px\">PS C:ps-test&gt; Invoke-Expression -Command &#8220;111 * 111&#8221;<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">12321<\/p>\n<p class=\"Code\">&nbsp;<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">PS C:ps-test&gt; &#8220;111 * 111&#8221; | Invoke-Expression<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">12321<\/p>\n<p class=\"Code\">&nbsp;<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">PS C:ps-test&gt; &#8220;111 * 111&#8221; | iex<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">12321<\/p>\n<ul>\n<li><strong>Formatting strings<span style=\"font-size:12px\">&nbsp;<\/span><\/strong><\/li>\n<\/ul>\n<p class=\"Code\" style=\"margin-left:60px\">PS C:&gt; $n = &#8220;111&#8221;<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">PS C:&gt; $n + &#8221; + &#8221; + $n<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">111 + 111<\/p>\n<p class=\"Code\">&nbsp;<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">PS C:&gt; &#8220;{0} + {0}&#8221; -f $n<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">111 + 111<\/p>\n<ul>\n<li><strong>Aligning formatted strings<\/strong><\/li>\n<\/ul>\n<p class=\"Code\" style=\"margin-left:60px\">PS C:&gt; &#8220;{0} + {0}&#8221; -f $n<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">111 + 111<\/p>\n<p class=\"Code\">&nbsp;<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">PS C:&gt; &#8220;{0, 5} + {0, 5}&#8221; -f $n<\/p>\n<p class=\"Code\" style=\"margin-left:60px\">&nbsp; 111 +&nbsp;&nbsp; 111\nNot bad lessons for quick Facebook post. Thanks, Doug!\nWe invite you to follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow.\n<b>June Blender, <\/b>Honorary Scripting Guy<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: June Blender explains Doug Finke&#8217;s multiplication and formatting trick. Today&#8230;the method. Microsoft Scripting Guy, Ed Wilson, is here. This is the second part of a two-part series written by June Blender, Honorary Scripting Guy. In the first part, Fun Formatting Ones&mdash;Part 1: The Task, we discussed the task of printing Doug&#8217;s multiplication table. In [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[56,370,3,4,336,45],"class_list":["post-639","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-june-blender","tag-scripting-guy","tag-scripting-techniques","tag-strings","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: June Blender explains Doug Finke&#8217;s multiplication and formatting trick. Today&#8230;the method. Microsoft Scripting Guy, Ed Wilson, is here. This is the second part of a two-part series written by June Blender, Honorary Scripting Guy. In the first part, Fun Formatting Ones&mdash;Part 1: The Task, we discussed the task of printing Doug&#8217;s multiplication table. In [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/639","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=639"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/639\/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=639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=639"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}