Skip to content

Instantly share code, notes, and snippets.

@kayasax
Created June 23, 2015 06:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kayasax/4a5aadbc36e3c0b81da4 to your computer and use it in GitHub Desktop.
Save kayasax/4a5aadbc36e3c0b81da4 to your computer and use it in GitHub Desktop.
powershell -f operator (from http://ss64.com/ps/syntax-f-operator.html )
-f Format operator
Format a string expression.
Syntax:
"String with placeholders" -f "Array of values to place into the placeholders"
'Filename: {0} Created: {1}' -f $_.fullname,$_.creationtime
"{I,A:FS} {I,A:FS} {I,A:FS}.." -f "string0", "string1", "string2"...
Place {0} {1} etc. into the string as placemarkers where you want the variables to appear, immediately follow the string with the -f operator and then lastly, a list of comma separated variables which will be used to populate the placemarkers.
Key:
I Index of the item to display, 0,1,2 etc.
A Alignment.
A positive number will right align n characters.
A negative number will left align n characters.
so {2,-25} will allocate 25 characters of horizontal space on the line, even if the string is only 1 character long.
FS An optional format string that acts on the item depending on its type (not case sensitive).
Valid format strings:
:c Currency format
:e Scientific (exp) notation
:f Fixed point
:f5 = fix to 5 places
:g Most compact format, fixed or sci
:g5 = 5 significant digits
:n Number (:nP precision=number of decimal places), includes culture separator for thousands 1,000.00
:p percentage
:r reversible precision
:x Hex format
:hh
:mm
:ss Convert a DateTime to a 2 digit Hour/minute/second
"{0:hh:0:mm}"
:ddd Convert a DateTime to Day of the Week
A full list of Date Formats
The format string can be surrounded by either single or double quotes.
Static text may be included before or in-between the -f {format strings.}
Examples:
Convert a number to Hex:
PS C:\> "{1,10} {0,10} {2,10:x}" -f "First", "Second", 255
Second First FF
Display filenames and creation time:
PS C:\> Get-ChildItem c:\docs | ForEach-Object {'Filename: {0} Created: {1}' -f $_.fullname,$_.creationtime}
Display the hours and minutes from a date time value:
PS C:\> "{0:hh}:{0:mm}" -f (Get-Date)
17:52
Left and right align text:
PS C:\> "|{0,-10}| |{1,10}|" -f "hello", "world"
|hello || world|
Reverse the order of display:
PS C:\> "{2} {1,-10} {0:n3}" -f [math]::pi, "world", "hello"
hello world 3.142
“I skate to where the puck is going to be, not where it has been” ~ Wayne Gretsky
Related:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment