{"id":10251,"date":"2006-06-16T14:41:00","date_gmt":"2006-06-16T14:41:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2006\/06\/16\/using-format-control-strings\/"},"modified":"2019-02-18T13:21:41","modified_gmt":"2019-02-18T20:21:41","slug":"using-format-control-strings","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/using-format-control-strings\/","title":{"rendered":"Using Format Control Strings."},"content":{"rendered":"<p>PSMDTAG:FAQ: How do I display large numbers with commas?<br \/>PSMDTAG:FAQ: How do I use .NET formatting strings?<\/p>\n<p>As scripters, we often spend tons of times trying to get our data formated just the right way.&nbsp; Often this is an error-prone, frustrating experience.&nbsp; Windows PowerShell leverages .NET objects to make this task a ton simpler.&nbsp; In .NET, every type supports ToString() and&nbsp;many of them support rich ToString Methods taking control strings.<\/p>\n<p>To tell if an object supports a format control&nbsp;string, just take an instance of the object and pipe it to Get-Member asking for the ToString member:<\/p>\n<p><font face=\"Courier New\" size=\"1\">PS&gt; $GUID = [GUID]::NewGUID()<br \/>PS&gt; $DATE = get-date<br \/>PS&gt; $GUID,$DATE |get-member ToString |ft -wrap<\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\">&nbsp;&nbsp; TypeName: System.Guid<\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\">Name&nbsp;&nbsp;&nbsp;&nbsp; MemberType Definition<br \/>&#8212;-&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<br \/>ToString Method&nbsp;&nbsp;&nbsp;&nbsp; System.String ToString(), System.String ToString(Strin<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; g <font color=\"#000000\"><strong>format<\/strong><\/font>), System.String ToString(String format, IForm<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; atProvider provider)<\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\">&nbsp;&nbsp; TypeName: System.DateTime<\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\">Name&nbsp;&nbsp;&nbsp;&nbsp; MemberType Definition<br \/>&#8212;-&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<br \/>ToString Method&nbsp;&nbsp;&nbsp;&nbsp; System.String ToString(), System.String ToString(Strin<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; g <strong><font>format<\/font><\/strong>), System.String ToString(IFormatProvider prov<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ider), System.String ToString(String format, IFormatPr<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ovider provider)<\/font><\/p>\n<p>If you examine the DEFINITION field you&#8217;ll see that both of these take a format string.<\/p>\n<p>Now to understand what the format string options are, you&#8217;ll have to look up the documentation for that type.&nbsp; Here is a link to the documentation for GUID <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpref\/html\/frlrfSystemGuidClassToStringTopic2.asp\">http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpref\/html\/frlrfSystemGuidClassToStringTopic2.asp<\/a><\/p>\n<p>PSTIP: The technique that I use to help find .NET documentation is to always include the term &#8220;members&#8221; in your search engine.&nbsp; <\/p>\n<p>Try searching for &#8220;GUID&#8221; and compare those results against the results of a search for &#8220;GUID Members&#8221;.&nbsp; You can seach for &#8220;GUID Members ToString&#8221; to get even closer.&nbsp; These links will show you the help for the class, you&#8217;ll want to link through to the ToString() method and then click through on the particular signature which takes the CONTROL STRING parameter and this will show you the values for the control string.&nbsp; Here is how you can use it:<\/p>\n<p><font face=\"Courier New\" size=\"1\">PS&gt; $GUID = [GUID]::NewGUID()<br \/>PS&gt; foreach ($S in &#8220;N&#8221;,&#8221;D&#8221;,&#8221;B&#8221;,&#8221;P&#8221;) {<br \/>&gt;&gt; &#8220;GUID Formatted with $S : {0}&#8221; -f $GUID.ToString($S)<br \/>&gt;&gt; }<br \/>&gt;&gt;<br \/>GUID Formatted with N : 0388d55e648046fea31d95d167fb36e6<br \/>GUID Formatted with D : 0388d55e-6480-46fe-a31d-95d167fb36e6<br \/>GUID Formatted with B : {0388d55e-6480-46fe-a31d-95d167fb36e6}<br \/>GUID Formatted with P : (0388d55e-6480-46fe-a31d-95d167fb36e6)<\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\"><\/font>&nbsp;<\/p>\n<p><font face=\"Courier New\" size=\"1\">PS&gt; $DATE = get-date<br \/>PS&gt; foreach ($S in &#8220;d&#8221;,&#8221;D&#8221;,&#8221;f&#8221;,&#8221;F&#8221;,&#8221;g&#8221;,&#8221;G&#8221;,&#8221;m&#8221;,&#8221;r&#8221;,&#8221;s&#8221;,&#8221;t&#8221;,&#8221;T&#8221;,&#8221;u&#8221;,&#8221;U&#8221;,<br \/>&gt;&gt; &#8220;y&#8221;,&#8221;dddd, MMMM dd yyyy&#8221;,&#8221;M\/yy&#8221;,&#8221;dd-MM-yy&#8221;) {<br \/>&gt;&gt; &#8220;DATE formatted with $S : {0}&#8221; -f $DATE.ToString($S)<br \/>&gt;&gt; }<br \/>&gt;&gt;<br \/>DATE formatted with d : 6\/16\/2006<br \/>DATE formatted with D : Friday, June 16, 2006<br \/>DATE formatted with f : Friday, June 16, 2006 11:57 AM<br \/>DATE formatted with F : Friday, June 16, 2006 11:57:36 AM<br \/>DATE formatted with g : 6\/16\/2006 11:57 AM<br \/>DATE formatted with G : 6\/16\/2006 11:57:36 AM<br \/>DATE formatted with m : June 16<br \/>DATE formatted with r : Fri, 16 Jun 2006 11:57:36 GMT<br \/>DATE formatted with s : 2006-06-16T11:57:36<br \/>DATE formatted with t : 11:57 AM<br \/>DATE formatted with T : 11:57:36 AM<br \/>DATE formatted with u : 2006-06-16 11:57:36Z<br \/>DATE formatted with U : Friday, June 16, 2006 6:57:36 PM<br \/>DATE formatted with y : June, 2006<br \/>DATE formatted with dddd, MMMM dd yyyy : Friday, June 16 2006<br \/>DATE formatted with M\/yy : 6\/06<br \/>DATE formatted with dd-MM-yy : 16-06-06<br \/>PS&gt;<\/font><\/p>\n<p>Format strings are great for Admins.&nbsp; Let me prove it.&nbsp; Quick &#8211; what is the number below<\/p>\n<p>10000000000<\/p>\n<p>Is that a million, ten million, a hundred million?&nbsp; With format strings you can do the following<\/p>\n<p>PS&gt; &#8220;{0:N0}&#8221; -f 10000000000<br \/>10,000,000,000<\/p>\n<p>I&#8217;ve heard that &#8220;diamonds may be a girl&#8217;s best friend&#8221;.&nbsp; I don&#8217;t know about that but I do know this:&nbsp;&#8220;commas are an Admin&#8217;s best friend&#8221;!<\/p>\n<p>So take few minutes to explore how to use format strings.&nbsp; Here is a script you can run to find all the classes which support format strings in your current process:<\/p>\n<p><font face=\"Courier New\" size=\"1\">[appdomain]::currentdomain.getassemblies() | % {<br \/>&nbsp;&nbsp; $_.GetExportedTypes() |where {! $_.IsSubclassof([System.Enum])}<br \/>&nbsp; } | % { <br \/>&nbsp;&nbsp;&nbsp;&nbsp; $Methods = $_.getmethods() |where {$_.name -eq &#8220;tostring&#8221;} |%{&#8220;$_&#8221;};<br \/>&nbsp;&nbsp;&nbsp;&nbsp; if ($methods -eq &#8220;System.String ToString(System.String)&#8221;) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $_.fullname<br \/>&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>&nbsp;&nbsp; }<br \/><\/font><\/p>\n<p>Enjoy<br \/>Jeffrey Snover<br \/>Windows PowerShell Architect<\/p>\n<p>PSMDTAG:DOTNET: Format strings<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PSMDTAG:FAQ: How do I display large numbers with commas?PSMDTAG:FAQ: How do I use .NET formatting strings? As scripters, we often spend tons of times trying to get our data formated just the right way.&nbsp; Often this is an error-prone, frustrating experience.&nbsp; Windows PowerShell leverages .NET objects to make this task a ton simpler.&nbsp; In .NET, [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[14,10],"class_list":["post-10251","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-dotnet","tag-faq"],"acf":[],"blog_post_summary":"<p>PSMDTAG:FAQ: How do I display large numbers with commas?PSMDTAG:FAQ: How do I use .NET formatting strings? As scripters, we often spend tons of times trying to get our data formated just the right way.&nbsp; Often this is an error-prone, frustrating experience.&nbsp; Windows PowerShell leverages .NET objects to make this task a ton simpler.&nbsp; In .NET, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=10251"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10251\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=10251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=10251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=10251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}