Syntax {N:02}

This topic contains 10 replies, has 4 voices, and was last updated by Profile photo of Rob Simmers Rob Simmers 1 year, 3 months ago.

  • Author
  • #27610
    Profile photo of Graham Beer
    Graham Beer

    Just bought Richard Siddaway powershell and WMI (so many powershell books and no time to read !) and in a lot of the queries there is things like this, {N:02}, in it. When I had a play it seems to put , between numbers. Is this regex? Can someone help me understand please ? Thanks as always !

  • #27612
    Profile photo of Dave Wyatt
    Dave Wyatt

    That's a fairly large topic, but the short answer is that it's a .NET format string. You can start reading up on this here: MSDN Link , or by reading the documentation on the String.Format() method.

    PowerShell's "-f" operator is just a wrapper around String.Format.

  • #27623
    Profile photo of Graham Beer
    Graham Beer

    Thanks Dave

  • #27627
    Profile photo of Rob Simmers
    Rob Simmers

    A small tutorial:

    #Here we have 2 variables in an array, Jim and Football.
    #0, the first item in the array is Jim, so we are placing
    #value Jim in the 0 place holder...
    "See {0} run with the {1}" -f "Jim", "Football"
    #Output:  See Jim run with the Football
    #As these are place holders, they don't have to be in order in your string.
    #What matters is the order of the variables in the array, so 1 in represented by
    #the day of the week which we not only use twice, but put it wherever in the string
    #we need it
    "Today is {1}, isn't that great {0}.  Yay, it's {1}!!"  -f "Frank", (Get-Date).DayOfWeek
    #Output:  Today is Friday, isn't that great Frank.  Yay, it's Friday!!
    #Now, if we start getting more advanced, we can use string formatting to format numbers,
    #dates, etc. to format them the way we want them.  A common one is a date, so...
    #Output: Friday, July 17, 2015 10:40:35 AM
    #If we wanted a custom date format, the 0 still represents the array and after the colon
    #we specify the string format that we want to convert the date to
    "{0:MM/dd/yy H:mm:ss zzz}" -f (Get-Date)
    #Output: 07/17/15 10:41:23 -04:00
    #Another simple string format is leading zeros.  0 is still our placeholder for the variable we're passing (3)
    #D is decimal value and 4 is the length, so no matter what number you pass it will be 4 digits in lenth
    "{0:D4}" -f 3
    "{0:D4}" -f 643
    #Personally, I like this
    $path = "{0}\MyTempFile.log" -f $Env:Temp
    #over this:
    $path = "$Env:Temp\MyTempFile.log"
    $path = $Env:Temp + "\MyTempFile.Log"
    #They all do the same thing, but I think the string format is cleaner and
    #you can create complex strings without escaping characters, doing double or triple qoutes,
    #or other tricks just to create a string
  • #27628
    Profile photo of Graham Beer
    Graham Beer

    Rob, that is extremely helpful. Thank you taking the time to write that.
    Certainly made it a bit clearer. So where D is for decimal what does the N stand for? The 0 is simply a placeholder then ? If 0 then that's the first holder ?

  • #27630
    Profile photo of Rob Simmers
    Rob Simmers

    N indicates a number format. See:

    For instance you could format 1234 to 1,234.00 with:

    "{0:N2}" -f 1234
  • #27633
    Profile photo of Graham Beer
    Graham Beer

    Arrr that's interesting. So the use of .net can be helpful with powershell (I know powershell is built on .net).

  • #27637
    Profile photo of Rob Simmers
    Rob Simmers

    A simple way to understand is to just see what the raw data is and then do the conversion. Just like in my examples, I show you Get-Date output and then the formatted input. You'll get the general idea of what the conversion is doing and have some handy tricks for your scripting later.

  • #27639
    Profile photo of Tim Curwick
    Tim Curwick


    While formatting in this way can be quite useful at times, I generally try to avoid it for the same reason you started the thread in the first place; it isn't very intuitive. Some people prefer it, but those tend to be people that have a history with another programming language that uses it or something similar, so they are used to it. If you are starting with PowerShell, use what works best in PowerShell and makes the most sense to you (and your team).

    There is usually a different way to do things that is easier to write, easier to read, and easier to maintain.

  • #27642
    Profile photo of Graham Beer
    Graham Beer

    Thanks Rob. Interesting perspective on things Tim, thanks. I'm following Richard Siddaway book on powershell and WMI. Uses some of these examples to present the query results. I just curious to know what it is and what it was doing. Seems clever, but does take some looking up. Do people remember all these little nuggets (0:n2 for example ) ?

  • #27670
    Profile photo of Rob Simmers
    Rob Simmers

    That's (one of) the benefit of the internet, a reference library. When you're working on a script one day and need it to be formatted with decimal places, now you'll remember you did that with string formatting and use your Google Fu or Tae Kwan Bing to find the documentation on how to do it. I remember the typical properties of common WMI classes, but I don't remember every class and certainly not every property. It's about learning how to do things so that you don't have to do unnecessary work later.

You must be logged in to reply to this topic.