powershell powershell -f operator(来自http://ss64.com/ps/syntax-f-operator.html)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell powershell -f operator(来自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:

以上是关于powershell powershell -f operator(来自http://ss64.com/ps/syntax-f-operator.html)的主要内容,如果未能解决你的问题,请参考以下文章

powershell PowerShell:启动PowerShell作为其他用户提升

powershell [提示输入powershell] #powershell #input #prompt

powershell 测量PowerShell命令或PowerShell脚本的速度

powershell远程下载exe并执行

powershell 使用啥端口

如何在不卸载powershell的情况下,有效禁用/启用powershel