{"id":65303,"date":"2007-03-16T01:44:00","date_gmt":"2007-03-16T01:44:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/03\/16\/how-can-i-format-a-series-of-numbers-as-a-phone-number\/"},"modified":"2007-03-16T01:44:00","modified_gmt":"2007-03-16T01:44:00","slug":"how-can-i-format-a-series-of-numbers-as-a-phone-number","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-format-a-series-of-numbers-as-a-phone-number\/","title":{"rendered":"How Can I Format a Series of Numbers as a Phone Number?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> <\/H2>\n<P>Hey, Scripting Guy! How can I format a series of numbers as a phone number in Windows PowerShell?<BR><BR>&#8212; HR<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, HR. You know, formatting is all about making things look a little nicer and a little more presentable. That\u2019s why the Scripting Guy who writes this column always finds it strange when people ask <I>him<\/I> formatting questions. A little nicer and a little more presentable? Right now the Scripting Guy who writes this column is wearing a Homer Simpson T-shirt that says, \u201cIf something is hard it\u2019s not worth doing.\u201d He\u2019s also wearing a ratty old long-sleeved shirt over the T-shirt; a pair of jeans; and a pair of three-year-old tennis shoes (although the shoelaces are brand-new). His entire ensemble is topped off by a half-smashed baseball hat that has <I>Japan<\/I> written across the front. And you want <I>him<\/I> telling you how to make things look a little nicer and a little more presentable?<\/P>\n<TABLE class=\"dataTable\" id=\"EFD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Note<\/B>. Here\u2019s something really scary: if you think the Scripting Guy who writes this column looks bad, well, you should see the other Scripting Guys.<\/P>\n<P>No, on second thought, maybe you shouldn\u2019t.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>But what the heck, HR; let\u2019s see if we can help you with your formatting question anyway. Suppose you\u2019ve type the following command into the Windows PowerShell console window:<\/P><PRE class=\"codeSample\">$a = 5551234567\n<\/PRE>\n<P>That\u2019s nice, but what you\u2019d really like to do is reformat $a so that it looks like this:<\/P><PRE class=\"codeSample\">(555) 123-4567\n<\/PRE>\n<P>So how can you do that? Why, by using this custom formatting string, of course:<\/P><PRE class=\"codeSample\">$b = &#8220;{0:(###) ###-####}&#8221; -f $a\n<\/PRE>\n<P>Let\u2019s see if we can explain how this works. When it comes to formatting numbers and dates Windows PowerShell has no dedicated Cmdlets equivalent to the VBScript FormatDate or FormatNumber functions; instead, PowerShell taps into the .NET Framework when formatting values. The drawback to using .NET is that it requires the crazy-looking syntax shown above. The advantage to using .NET? You can create custom formats for displaying data in all sorts of different ways, including taking a series of numbers and displaying them as a phone number.<\/P>\n<P>In order to understand this a little better let\u2019s dissect our formatting command. What we\u2019re doing with this command is applying a format to the variable $a; the gobbledygook between the double quote marks is the format being applied (details to follow), and the <B>\u2013f<\/B> parameter is the signal we use to tell Windows PowerShell that we\u2019re doing some formatting. The value to be formatted ($a) comes after the \u2013f parameter, and the newly-formatted value is assigned to a variable named $b. And no, we don\u2019t actually have to assign the newly-formatted value to a variable; if we wanted to we could use a command like this and simply display that value:<\/P><PRE class=\"codeSample\">&#8220;{0:(###) ###-####}&#8221; -f $a\n<\/PRE>\n<P>Now, what about the crazy-looking syntax? As you can see, the entire formatting string is nested first between a pair of double quote marks and then between a pair of curly braces: <B>&#8220;{<\/B>0:(###) ###-####<B>}&#8221;<\/B> We then have a 0 followed by a colon: <B>0:<\/B>. That simply tells Windows PowerShell that we want to format the first item (item 0). For now consider that to be the default, and just use <I>0:<\/I> without worrying too much about it.<\/P>\n<P>The part that you <I>do<\/I> need to worry about (or at least the part you need to understand) is the actual formatting string itself: <B>(###) ###-####<\/B>. If you\u2019re thinking, \u201cYou know, that kind of looks like a phone number,\u201d well, that\u2019s good; after all, it\u2019s <I>supposed<\/I> to look like a phone number. In this formatting string the pound signs (#) each represent a digit; the parentheses and hyphens represent parentheses and hyphens. What we\u2019re saying here is that we want to use the following format: <\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>An open parentheses followed by three digits.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>A closed parentheses and a space.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Three digits and a hyphen.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>The final four digits.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>That means we\u2019re going to take a value like 5551234567 and display it like this:<\/P><PRE class=\"codeSample\">(555) 123-4567\n<\/PRE>\n<P>Lo and behold, there\u2019s our phone number.<\/P>\n<P>As long as we\u2019re on the subject here are a couple of other custom formatting commands you might find useful. For example, suppose you want to format a value using leading zeroes. Well, here\u2019s an example for you:<\/P><PRE class=\"codeSample\">$a = 123\n&#8220;{0:000000}&#8221; -f $a\n<\/PRE>\n<P>What are you going to get when you run these two commands? This:<\/P><PRE class=\"codeSample\">000123\n<\/PRE>\n<P>How about a fixed number of decimal places? Are three decimal places enough? Okey-doke:<\/P><PRE class=\"codeSample\">$a = 123.456789\n&#8220;{0:#.000}&#8221; -f $a\n<\/PRE>\n<P>That will result in output that looks like this (note that the value has been rounded up to the nearest decimal place):<\/P><PRE class=\"codeSample\">123.457\n<\/PRE>\n<P>Here\u2019s one that uses U.S. currency as the format; note the <I>$<\/I> symbol included at the beginning of the formatting string:<\/P><PRE class=\"codeSample\">$a = 123.45\n&#8220;{0:$#.00}&#8221; -f $a\n<\/PRE>\n<P>That yields a value the looks like this:<\/P><PRE class=\"codeSample\">$123.45\n<\/PRE>\n<P>You can do similar things for dates. But that\u2019s another story for another day.<\/P>\n<P>We hope that helps, HR. And remember, if you ever need any other advice on making things look nicer and a little more presentable the Scripting Guys will be here. Take interior decorating, for example. The Scripting Guy who writes this column has an office that has nothing hanging on the walls other than a map of the world, a map which is hidden behind his door and is hanging there simply because the previous tenant left it hanging there. He has no plants, no photographs, no personal mementoes, nothing. <\/P>\n<TABLE class=\"dataTable\" id=\"EWF\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Well, OK, with one exception: taped to his door is a fortune from a Chinese fortune cookie that says, \u201cOthers take notice of your radiance. Share your happiness.\u201d The other Scripting Guys agree that they\u2019ve never heard a more apt description of the Scripting Guy who writes this column.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Best of all, following the last office move he didn\u2019t even bother to unpack his things; instead, two months after the move all his unopened boxes remain piled up in one corner of the office. If you\u2019d like to have an office like that, well, just let him know; we\u2019re sure he\u2019d be happy to help. After all, sharing his happiness is what he does best.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I format a series of numbers as a phone number in Windows PowerShell?&#8212; HR Hey, HR. You know, formatting is all about making things look a little nicer and a little more presentable. That\u2019s why the Scripting Guy who writes this column always finds it strange when people ask him [&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":[25,3,4,45],"class_list":["post-65303","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-displaying-output","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I format a series of numbers as a phone number in Windows PowerShell?&#8212; HR Hey, HR. You know, formatting is all about making things look a little nicer and a little more presentable. That\u2019s why the Scripting Guy who writes this column always finds it strange when people ask him [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65303","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=65303"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65303\/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=65303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}