{"id":55443,"date":"2008-06-02T18:12:00","date_gmt":"2008-06-02T18:12:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/06\/02\/hey-scripting-guy-how-can-i-use-leading-zeroes-when-displaying-a-value-in-windows-powershell\/"},"modified":"2008-06-02T18:12:00","modified_gmt":"2008-06-02T18:12:00","slug":"hey-scripting-guy-how-can-i-use-leading-zeroes-when-displaying-a-value-in-windows-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-use-leading-zeroes-when-displaying-a-value-in-windows-powershell\/","title":{"rendered":"Hey, Scripting Guy! How Can I Use Leading Zeroes When Displaying a Value in Windows PowerShell?"},"content":{"rendered":"<h2><img decoding=\"async\" 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 just read your \u201c<a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/pstips\/apr08\/pstip0425.mspx\"><b>using calculated properties<\/b><\/a>\u201d PowerShell Tip of the Week, and it addresses a problem I recently had. However, when I got to the last step in your article, I realized the file sizes \u2013 and my displayed values \u2013 are now left-justified. I\u2019d like to use leading zeroes to align the numbers on the decimal point, but I haven\u2019t figured out how to do that. Any suggestions?<br \/>&#8212; GD<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\" \/><img decoding=\"async\" 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\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\" \/><\/a> <\/p>\n<p>Hey, GD. You know, any time we answer a Windows PowerShell question we usually end up getting a flurry of PowerShell-related questions, all of them pretty much like this one:<\/p>\n<p>Hey, Scripting Guy! I saw your column about Windows PowerShell. That\u2019s something I\u2019d really like to learn; do you have any suggestions on a good starting place for someone brand-new to Windows PowerShell?<\/p>\n<p>Well, as a matter of fact we do \u2013 now. Next week (next Tuesday \u2013 June 10, 2008, in fact) the Scripting Guys will be delivering a pair of instructor-led labs at <a href=\"http:\/\/www.microsoft.com\/events\/teched2008\/itpro\/default.mspx\" target=\"_blank\"><b>TechEd 2008<\/b><\/a> in Orlando. We\u2019ll be presenting Windows PowerShell for VBScripters at 10:00 AM on Tuesday, then we\u2019ll present the same lab again at 1:00 PM, although this late show will be for adults only.<\/p>\n<p>Although, now that we think about it, kids aren\u2019t allowed to attend TechEd. Which means that the early show will be for adults only, too. <\/p>\n<p>At any rate, this lab is designed for people \u2013 <i>adult<\/i> people \u2013 who are new to Windows PowerShell. (Despite the name, no VBScript experience is required.) What will we do during the lab? Well, we have only an hour and 15 minutes for each session, which means we\u2019re going to try and cram as much PowerShell knowledge into your heads as we can. And if we can\u2019t get to something? Hey, no problem: we also have some useful handouts for you, including a printed \u2013 and greatly expanded \u2013 <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/winpsh\/convert\/default.mspx\"><b>VBScript to Windows PowerShell Conversion Guide<\/b><\/a>, as well as a printed \u2013 and slightly expanded \u2013 version of the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/winpsh\/manual\/default.mspx\"><b>Windows PowerShell Owners Manual<\/b><\/a>. Are you going to get copies of these two booklets at any other session in TechEd? Are you going to get copies of these two booklets at Universal Studios or Disney World? In a word: no.<\/p>\n<table id=\"E2D\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\"><b>Note<\/b>. Will you get copies of these two booklets at Sea World? Hmmm; good question. We\u2019ll have to get back to you on that. They <i>seem<\/i> like the sort of thing you\u2019d get at Sea World, but we can\u2019t be sure.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Oh, and even <i>that\u2019s<\/i> not all. At the end of each lab we\u2019ll also have a special guest appearance by Hal Rottenberg, co-host of the ever-fascinating <a href=\"http:\/\/powerscripting.net\/\" target=\"_blank\"><b>PowerShell Podcast<\/b><\/a>, and Community Director for the Web site <a href=\"http:\/\/powershellcommunity.org\/\" target=\"_blank\"><b>PowerShellCommunity.org<\/b><\/a>. Hal\u2019s going to talk about the Windows PowerShell community, something which will definitely be of interest to those of you just getting started with Windows PowerShell.<\/p>\n<table id=\"E1E\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p><b>Note<\/b>. And what if you\u2019re an old hand at Windows PowerShell? Well, you could lend newcomers a hand by suggesting resources we could add to our <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/winpsh\/toolbox.mspx\"><b>Windows PowerShell Toolbox<\/b><\/a> and the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/winpsh\/tools\/guide.mspx\"><b>Windows PowerShell Holiday Gift Guide<\/b><\/a>. If you know of a good PowerShell utility, Web site, book, or other resource that we haven\u2019t listed, please drop us a line at <a href=\"mailto:scripter@microsoft.com?subject=PowerShell Toolbox\"><b>scripter@microsoft.com (in English, if possible)<\/b><\/a> and let us know. We thank you, Hal Rottenberg thanks you, and the newcomers to Windows PowerShell thank you.<\/p>\n<p>Well, except for that grouchy guy from Wisconsin, who never thanks anybody for anything. But all the other PowerShell newcomers thank you.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Of course, the one problem with all this is the fact that we don\u2019t present our instructor-led lab until next Tuesday. (Well, OK, another problem is the fact that this lab is only going to be presented at TechEd 2008, which means you\u2019re going to have to figure out a way to get into the conference and get yourself to Orlando before next Tuesday. But how hard could that be?) That means that you\u2019ve still got a week \u2013 <i>over<\/i> a week \u2013 to wait before the world premiere of Windows PowerShell for VBScripters. How are you supposed to get your Windows PowerShell fix between now and then? Here\u2019s a hint: just keep reading. We have all sorts of good PowerShell information for you in today\u2019s column.<\/p>\n<p>And no, this <i>isn\u2019t<\/i> the first time that a <i>Hey, Scripting Guy!<\/i> column has been filled with good information. After all, there was \u2026 that one time. And then that other time after that.<\/p>\n<p>Although, come to think of it, there really <i>wasn\u2019t<\/i> much information in that one, was there?<\/p>\n<p>At any rate, let\u2019s take a peek at a simple example and then \u2013 after we\u2019ve illustrated the basic concept \u2013 we\u2019ll show you how to use this same approach using a calculated property. (But no, we won\u2019t discuss calculated properties or how they are used, at least not in today\u2019s column. After all, that\u2019s what the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/pstips\/apr08\/pstip0425.mspx\"><b>PowerShell Tip of the Week<\/b><\/a> that GD referred to is for.)<\/p>\n<p>The problem GD has is that he\u2019s getting output that looks like this:<\/p>\n<pre class=\"codeSample\">37.222378\n5421.1991\n3.2\n<\/pre>\n<p>As you can see, that output is a bit difficult to decipher, especially at first glance. What GD would <i>like<\/i> to get is output that looks like this, with leading zeroes that ensure that the decimal places line up nice and neat:<\/p>\n<pre class=\"codeSample\">00037.222378\n05421.1991\n00003.2\n<\/pre>\n<p>How\u2019s he supposed to do that? Well, here\u2019s one suggestion:<\/p>\n<pre class=\"codeSample\">$a = 37.222378\n$a.ToString(\"00000.00\")\n$b = 5421.1991\n$b.ToString(\"00000.00\")\n$c = 3.2\n$c.ToString(\"00000.00\")\n<\/pre>\n<p>Good question: what <i>are<\/i> we doing here? Well, the first line should be fairly straightforward; all we\u2019re doing there is assigning the value 37.222378 to the variable $a. And the second line really isn\u2019t all that difficult, either: in that line we\u2019re using the <b>ToString<\/b> method to display the value of $a as a string. When we do that (display a number as a string), we can apply some standard formatting characters to the string as well. For this sample script, we want to do two things: 1) we want to display a maximum of 6 leading zeroes; and, 2) we want to display two decimal places. That\u2019s why we used the format string <b>000000.00<\/b>: the <b>000000<\/b> gives us six leading zeros (or, to be a little more precise, as many leading zeroes as we need in order to fill six digit spots) and the <b>.00<\/b> gives us two decimal places.<\/p>\n<p>We then do the same thing for the values assigned to $b and $c. And what\u2019s our output going to look? It\u2019s going to look an awful lot like this:<\/p>\n<pre class=\"codeSample\">00037.22\n05421.20\n00003.20\n<\/pre>\n<p>Of course, PowerShell being PowerShell, we also could have carried out this same task using a single command:<\/p>\n<pre class=\"codeSample\">($a = 37.222378, 5421.1991, 3.2) | ForEach-Object {$_.ToString(\"000000.00\")}\n<\/pre>\n<p>In this example we\u2019re simply assigning all three of our numeric values to an array named $a. We then pipe the value of $a to the <b>ForEach-Object<\/b> cmdlet, asking ForEach-Object to use the ToString method to display the value of each item in the array, and to display these values using the 000000.00 format.<\/p>\n<p>And yes, that <i>is<\/i> awfully nice of ForEach-Object to do that for us, isn\u2019t it?<\/p>\n<p>While we\u2019re on the subject, what if you wanted a fixed number of decimal places but didn\u2019t care about leading zeroes? Well, in that case you could always use a format string like this one:<\/p>\n<pre class=\"codeSample\">$a.ToString(\"#.000000\")\n<\/pre>\n<p>Here we\u2019re using the <b>#<\/b> sign to represent one or more digits, then using <b>.000000<\/b> to represent six decimal places. What\u2019s $a going to look like when we run this command? It\u2019s going to look like this:<\/p>\n<pre class=\"codeSample\">37.222378\n<\/pre>\n<p>Here\u2019s another formatting string you might find useful:<\/p>\n<pre class=\"codeSample\">$a.ToString(\"#,###.00\")\n<\/pre>\n<p>Notice the syntax here: <b>#,###.00<\/b>. The #,### is going to use a comma as the thousands separator; in other words the value 1,000 is going to be displayed like this:<\/p>\n<pre class=\"codeSample\">1,000.00\n<\/pre>\n<p>And it doesn\u2019t matter how many thousands (or tens of thousands or hundreds of thousands or thousands of thousands) you might have; PowerShell will put in as many commas as needed. For example, take this little block of code:<\/p>\n<pre class=\"codeSample\">$a = 11366777.222378\n$a.ToString(\"#,###.00\")\n<\/pre>\n<p>As you can see, the value 11366777.222378 needs all sorts of commas in order to separate the thousands. That\u2019s fine; PowerShell will take care of that for you:<\/p>\n<pre class=\"codeSample\">11,366,777.22\n<\/pre>\n<p>Now, what about using this approach in a calculated property? Well, as it turns out, that\u2019s no big deal:<\/p>\n<pre class=\"codeSample\">Get-ChildItem C:\\Scripts | Where-Object {$_.PsISContainer -eq $False } | \nSelect-Object Name, @{Name=\"Kbytes\";Expression={ ($_.Length \/ 1KB).ToString(\"00000.0\") } }\n<\/pre>\n<p>Again we\u2019re too lazy \u2013 um, too pressed for time to discuss calculated properties in any depth today. But take a look at our calculated property for displaying file sizes in kilobytes:<\/p>\n<pre class=\"codeSample\">@{Name=\"Kbytes\";Expression={ ($_.Length \/ 1KB).ToString(\"00000.0\") }\n<\/pre>\n<p>In this case we\u2019re taking the <b>Length<\/b> (size, in bytes) of each file and dividing it by the numeric constant 1KB; that gives us the size of each file in bytes. We then take that value and display it using the ToString method, this time using 5 leading zeroes and a single decimal place:<\/p>\n<pre class=\"codeSample\">ToString(\"00000.0\")\n<\/pre>\n<p>And what will that result in? This:<\/p>\n<pre class=\"codeSample\">Name                                                        Kbytes\n----                                                        ------\nAverage.txt                                                 00001.8\nbcdscripts.zip                                              00006.6\nbootmgr.vbs                                                 00001.9\nctp2.txt                                                    00035.1\nctp2_cmdlets.txt                                            00111.5\nhey0501.doc                                                 00115.0\nmimic_bcdedit.vbs                                           00017.5\nMORPHEUS.TTF                                                00066.4\nos_info.vbs                                                 00017.5\noutput.txt                                                  00032.9\npool.mdb                                                    00224.0\nPowerShellScriptOMaticCompiledRelease.exe                   00430.6\n<\/pre>\n<p>Pretty nice, huh?<\/p>\n<p>But wait; there\u2019s more. Leading zeroes are nice, but it might be even nicer \u2013 at least in some cases \u2013 to have output that looks like this:<\/p>\n<pre class=\"codeSample\">   37.22\n 5421.20\n    3.20\n<\/pre>\n<p>In other words, we\u2019d like to use \u201cleading blank spaces\u201d rather than leading zeroes. But is that even possible using out .NET formatting characters?<\/p>\n<p>To tell you the truth, we aren\u2019t sure; however, after a brief search we weren\u2019t able to come up with anything. But that\u2019s OK; instead, we came up with this:<\/p>\n<pre class=\"codeSample\">$a = 37.222378\n($a.ToString(\"#.00\")).PadLeft(10)\n$b = 5421.1991\n($b.ToString(\"#.00\")).PadLeft(10)\n$c = 3.2\n($c.ToString(\"#.00\")).PadLeft(10)\n<\/pre>\n<p>This time around we\u2019re doing \u201ctwo-step\u201d formatting. In step 1, we use the ToString method to convert the value of each variable to a formatted string, in this case a number (or set of numbers) followed by two decimal places:<\/p>\n<pre class=\"codeSample\">($a.ToString(\"#.00\"))\n<\/pre>\n<p>Note that we placed this command in parentheses to ensure that PowerShell carried this out before doing anything else.<\/p>\n<p>Step 1 converts the value 37.222378 to this: 37.22. In step 2, we\u2019re going to use the <b>PadLeft<\/b> method to insert enough blank spaces at the beginning of the string to ensure that the string fills up 10 character spaces. That makes $a equal to this:<\/p>\n<pre class=\"codeSample\">     37.22\n<\/pre>\n<p>And when we apply the same process to $b and $c we end up getting the \u201cleading blank spaces\u201d we were after:<\/p>\n<pre class=\"codeSample\">   37.22\n 5421.20\n    3.20\n<\/pre>\n<p>Pretty slick, huh?<\/p>\n<p>That should do it, GD. If you have any other questions about Windows PowerShell, just stop by TechEd 2008 next week and we\u2019ll be happy to help you. As we noted, we\u2019ll be delivering a pair of instructor-led labs on Tuesday; we\u2019ll also be available after each session to answer questions and to chat about scripting. (Well, as far as we know we\u2019ll be available.) In addition, we\u2019ll be hanging out at the TechNet Magazine booth on Wednesday morning; if you get the chance, drop by and say hi.<\/p>\n<p>Oh, and don\u2019t forget about those new resources for the <a href=\"mailto:scripter@microsoft.com?subject=PowerShell Toolbox\"><b>PowerShell Toolbox<\/b><\/a>. You say that, as much as you enjoy helping people, you need more motivation that just that? OK, how about this: we have a handful of Dr. Scripto bobbleheads lying around. Send us a suggestion by Friday morning at 8:00 AM, and we\u2019ll toss your name into a hat. When we get back from Orlando we\u2019ll draw names and award bobbleheads to 5 lucky souls. But you have to send us a suggestion for the PowerShell Toolbox by Friday morning to be eligible.<\/p>\n<p>Deal? Deal.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I just read your \u201cusing calculated properties\u201d PowerShell Tip of the Week, and it addresses a problem I recently had. However, when I got to the last step in your article, I realized the file sizes \u2013 and my displayed values \u2013 are now left-justified. I\u2019d like to use leading zeroes to [&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-55443","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 just read your \u201cusing calculated properties\u201d PowerShell Tip of the Week, and it addresses a problem I recently had. However, when I got to the last step in your article, I realized the file sizes \u2013 and my displayed values \u2013 are now left-justified. I\u2019d like to use leading zeroes to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55443","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=55443"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55443\/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=55443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=55443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=55443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}