6

Typical string formatting in powershell for instance to use padding or specifying number can be written like this:

>>> "x={0,5} and y={1:F3}" -f $x, $y
x=   10 and y=0.333

But in Powershell you can also use string interpolation like

>>> $x=10
>>> $y=1/3
>>> "x=$x and y=$y"
x=10 and y=0.333333333333333

And in C# string interpolation also supports the formatting specifiers:

> var x = 10;
> var y = 1.0/3.0;
> $"x={x,5} and y = {y:F2}";
"x=   10 and y = 0.33"

Is there a way to have that in Powershell? I've tried many combinations like

>>> "var=$($var, 10)"
var=10 10

but none of them work. Is this supported? Or is there a succinct way to call into C# to use it?

update as Mathias answers and as confirmed on Powershell's github this is currently not supported, so I made a feature request here

6
  • I assume you mean "var={0,10}" -f $var like in my second code example. Well, because I like inline string interpolation, am used to the syntax from other languages, it's somewhat shorter and I find it more readable (usually). Commented Apr 4, 2020 at 11:05
  • 1
    Not clear what you are asking here.. Do you mean to left-pad the variable $var? In that case, you could use the String method PadLeft, something like "var=$($var.ToString().PadLeft(5))" Commented Apr 4, 2020 at 11:41
  • @Theo yes, padding, formatting numbers and dates etc, the usual C# things. See edit for clarification. Something like what you propse works of course, but it's not very succinct. Commented Apr 4, 2020 at 12:49
  • @stijn - from what i can tell ... NO. the only way to use string composition is to use the -f string format operator. there is [apparently] no other way in powershell to do that stuff. Commented Apr 4, 2020 at 13:42
  • "$(' '*10)$var" Commented Apr 4, 2020 at 14:30

2 Answers 2

7

Is this supported?

No, formatting is not supported during string expansion


As you might have noticed, string expansion in PowerShell works by naively resolving subexpressions nested in double-quoted strings - there are no {} placeholder constructs.

If you want string formatting, -f is the way to go.

FWIW, $s -f $a is directly translated to a String.Format($s, $a) call

For value types that support string formatting you can usually also call ToString() with a format string (just like in C#):

PS C:\> $a = 1 / 3 
PS C:\> $a.ToString("F2")
0.33
Sign up to request clarification or add additional context in comments.

Comments

0

The way to go with this is to use the -f operator. The -f operator takes a format string as it's first operand and the items you want to format as the second. The format string must contain tokens like {n:f} where n is the index of the item in the items to be formatted and f is the format specifier. For example {0:D4} will take the first item and format it as a Decimal that is a minimum of 4 digits. So '{0:D4}-{1:D3}' -f 24,3 gives the output 0024-003. ref

Inside an expandable string this can be used within a subexpression operator $().

A real world example is to split an input file into multiple files with ten lines in each while making the the output file name contain a serial number wich is padded with zeros to make it four digits. This can be done like this:

$i = 0; Get-Content input.txt -ReadCount 10 | %{ $i++; $_ | Out-File "output_$('{0:D4}' -f $i).txt" }

Creates files

output_0001.txt
output_0002.txt
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.