How to use String.Format in PowerShell?

Rahul Singh 1 Reputation point
2020-08-12T03:14:32.963+00:00

Hah! (We’re back ;-) ) It’s as easy as that:

PS > [string] :: Format( "Hello {0} {1} :-)", "Rahul", "Singh")
Hello Daniel Walzenbach :-)

Needless to say that other formatting – like Currency formatting – works as well

PS > [string] :: Format( "{0:C}", 1234567890)
$1,234,567,890.00

Check out Formatting Types on the .NET Framework Developer's Guide or SteveX Compiled - String Formatting in C# (nice cheat sheet) for more examples.

Cheers!

Rahul

Microsoft Edge Microsoft Edge development
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Leon Laude 86,026 Reputation points
    2020-08-12T06:23:19.49+00:00

    Hi,

    You have posted in the Microsoft Edge forum, since your issue is related to PowerShell I suggest you ask over at the dedicated PowerShell forum over here:
    https://learn.microsoft.com/en-us/answers/topics/windows-server-powershell.html

    ----------

    (If the reply was helpful please don't forget to accept as answer, thank you)

    Best regards,
    Leon

    0 comments No comments

  2. Bart van den Donk 1 Reputation point
    2021-01-13T08:16:55.337+00:00

    In Powershell there is an easier way:

    "{0:C}" -f 1234567890
    
    "Hello {0} {1} :-)" -f "Rahul", "Singh"
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.