{"id":8091,"date":"2015-01-29T00:01:00","date_gmt":"2015-01-29T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/01\/29\/using-powershell-and-standard-number-formats\/"},"modified":"2019-02-18T10:35:43","modified_gmt":"2019-02-18T17:35:43","slug":"using-powershell-and-standard-number-formats","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/using-powershell-and-standard-number-formats\/","title":{"rendered":"Using PowerShell and Standard Number Formats"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using standard number formats.<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! I love using Windows PowerShell. I use it for everything. My problem is that it does not do everything automatically. For example, I use the <b>Get-Volume<\/b> cmdlet a lot to check on disk space, but I need to do a manual calculation to see the percentage of free space. This involves basically writing a script and dividing the numbers, multiplying by a hundred, and then trimming all the excess decimal places. Is there anything you can do to help me?<\/p>\n<p>&mdash;AV<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello AV,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sitting here sipping a beautiful cup of Darjeeling tea. I added a cinnamon stick to it, and that is it. It is robust, complex, and deeply relaxing. I am also munching on a freshly made bagel, with just a bit of cream cheese on it. I am checking my email on my Surface Pro&nbsp;3, and looking outside wondering where the snow is. No snow here in Charlotte. Nope, not a flake.<\/p>\n<p>Anyway, on cool, crisp mornings such as this, it is nice to open the Windows PowerShell ISE and curl up with a nice script. However, AV, your problem doesn&rsquo;t need a scripted solution. You may want to write a quick script, but it is not totally necessary. As someone once said, I can solve that problem in one line.<\/p>\n<h2>Display formatted percentages<\/h2>\n<p>I can use the Windows PowerShell command <b>Get-Volume<\/b> (it is technically a function and not a cmdlet) to display a nice output that contains information about my drives. This command and its output are shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-1-29-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-1-29-15-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>One of the properties that appears to be missing is percentage of free space. I like to create a custom property for an object by using the <b>Select-Object<\/b> cmdlet. To do this, I use a hash table. I specify a label (a name for the property) and an expression (a script block that creates a value for that property). I can use the aliases <b>l<\/b> for label and <b>e <\/b>for expression. The hash table looks like this:<\/p>\n<p style=\"margin-left:30px\">@{l=&#039;percent free&#039;;e={($_.sizeremaining\/$_.size)}}<\/p>\n<p>When I do this, I can use the special number format code, <b>&ldquo;P&rdquo;<\/b>, to create my percentage. Basically, it multiplies my value by 100 and returns the first two places after the decimal point. It also rounds up or down as necessary. The number format code becomes an override for the <b>ToString<\/b><i> <\/i>method. In the following example, I show how to call the <b>ToString<\/b><i> <\/i>method. Note that the <b>&ldquo;P&rdquo;<\/b> goes inside the parentheses that are required for all method calls.<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; (5\/10).ToString(&quot;P&quot;)<\/p>\n<p style=\"margin-left:30px\">50.00 %<\/p>\n<p>To compute the percent free for each of my volumes, I pipe the <b>Get-Volume<\/b> function to the <b>Select-Object<\/b> cmdlet, and I specify the volume letter and my hash table as properties. Here is the command I use:<\/p>\n<p style=\"margin-left:30px\">Get-Volume | Select driveletter, @{l=&#039;percentfree&#039;;e={($_.sizeremaining\/$_.size).tostring(&quot;P&quot;)}}<\/p>\n<p>The command and the output from the command are shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-1-29-15-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-1-29-15-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>AV, that is all there is to using Windows PowerShell to display a formatted percentage. Numbers&nbsp; Week will continue tomorrow when I will talk about more cool Windows PowerShell stuff.<\/p>\n<p>I invite you to follow me 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 me at <a href=\"mailto: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. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using standard number formats. &nbsp;Hey, Scripting Guy! I love using Windows PowerShell. I use it for everything. My problem is that it does not do everything automatically. For example, I use the Get-Volume cmdlet a lot to check on disk space, but I need to do a [&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":[51,398,3,45],"class_list":["post-8091","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-numbers","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using standard number formats. &nbsp;Hey, Scripting Guy! I love using Windows PowerShell. I use it for everything. My problem is that it does not do everything automatically. For example, I use the Get-Volume cmdlet a lot to check on disk space, but I need to do a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8091","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=8091"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8091\/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=8091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=8091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=8091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}