每天powershell-outfile帮助文档翻译

摘要:Sends output to a file. 将输出发送到文件
语法:
Out-File [-FilePath] <System.String> [[-Encoding] {ASCII | BigEndianUnicode | Default |OEM | String | Unicode | Unknown | UTF7 | UTF8 | UTF32}] [-Append] [-Force] [-InputObject <System.Management.Automation.PSObject>] [-NoClobber] [-NoNewline] [-Width <System.Int32>] [-Confirm] [-WhatIf] []

Out-File [[-Encoding] {ASCII | BigEndianUnicode | Default | OEM | String | Unicode | Unknown | UTF7 | UTF8 | UTF32}] [-Append] [-Force] [-InputObject <System.Management.Automation.PSObject>] -LiteralPath <System.String> [-NoClobber] [-NoNewline] [-Width <System.Int32>] [-Confirm] [-WhatIf] []

参数:必须参数[-parameter]<-type>;可选参数[-parameter <-type>];开关参数[-parameter]

说明:
The Out-File cmdlet sends output to a file. When you need to specify parameters for the output use Out-File rather than the redirection operator (>).
这个'Out-File' cmdlet 是发送输出到一个文件。当你需要为输出指定参数时,请使用'Out-File'而不是重定向运算符('>')。

参数:
-Append<System.Management.Automation.SwitchParameter> (Append-附加;贴上)
Adds the output to the end of an existing file. If no Encoding is specified, the cmdlet uses the default encoding. That encoding may not match the encoding of the target file. This is the same behavior as the redirection operator (>>).
将输出添加到现有文件的末尾。如果未指定Encoding,则cmdlet将使用默认编码。该编码可能与目标文件的编码不匹配。这与重定向运算符的行为相同('>>')。

    是否必需?                    False
    位置?                        named
    默认值                False
    是否接受管道输入?            False
    是否接受通配符?              False

-Encoding <System.String> (Encoding-编码)
Specifies the type of encoding for the target file. The default value is unicode.
为目标文件指定编码类型,默认值为'unicode'.
The acceptable values for this parameter are as follows:
此参数的可接受值如下所示

    - `ascii` Uses ASCII (7-bit) character set.

    - `bigendianunicode` Uses UTF-16 with the big-endian byte order.

    - `default` Uses the encoding that corresponds to the system's active code page (usually ANSI).

    - `oem` Uses the encoding that corresponds to the system's current OEM code page.

    - `string` Same as `unicode`.

    - `unicode` Uses UTF-16 with the little-endian byte order.

    - `unknown` Same as `unicode`.

    - `utf7` Uses UTF-7.

    - `utf8` Uses UTF-8.

    - `utf32` Uses UTF-32 with the little-endian byte order.

    是否必需?                    False
    位置?                        1
    默认值                Unicode
    是否接受管道输入?            False
    是否接受通配符?              False

-FilePath <System.String> (FilePath-文件路径)
Specifies the path to the output file.
指定输出文件的路径

    是否必需?                    True
    位置?                        0
    默认值                None
    是否接受管道输入?            False
    是否接受通配符?              False

-Force <System.Management.Automation.SwitchParameter> (Force-强制)
Overrides the read-only attribute and overwrites an existing read-only file. The Force parameter does not override security restrictions.
覆盖只读属性并覆盖现有只读文件。Force 参数不会覆盖安全限制

    是否必需?                    False
    位置?                        named
    默认值                False
    是否接受管道输入?            False
    是否接受通配符?              False

-InputObject <System.Management.Automation.PSObject> (InputObject-输入对象)
Specifies the objects to be written to the file. Enter a variable that contains the objects or type a command or expression that gets the objects.
指定写入文件的对象。输入包含对象的变量或键入获取对象的命令或表达式。

    是否必需?                    False
    位置?                        named
    默认值                None
    是否接受管道输入?            True (ByValue)
    是否接受通配符?              False

-LiteralPath <System.String>  (LiteralPath-文字路径)
    Specifies the path to the output file. The LiteralPath parameter is used exactly asit is typed. Wildcard characters are not accepted. If the path includes escape characters, enclose it in single quotation marks. Single quotation marks tell PowerShell not to interpret any characters as escape sequences. For more information, see about_Quoting_Rules (../Microsoft.Powershell.Core/About/about_Quoting_Rules.md).
  指定输出文件的路径。LiteralPath参数的使用与键入时完全相同,不接受通配符,如果路径中包含转义字符,则将其用单引号引起来,引号告诉PowerShell不要将任何字符解释为转义序列,有关更多信息,请参见about_Quoting_Rules

    是否必需?          True
    位置?                   named
    默认值                None
    是否接受管道输入?            True (ByPropertyName)
    是否接受通配符?              False

-NoClobber <System.Management.Automation.SwitchParameter> (NoClobber-不直接覆盖并提示)
    NoClobber prevents an existing file from being overwritten and displays a message that the file already exists. By default, if a file exists in the specified path, `Out-File` overwrites the file without warning.
  NoClobber防止覆盖现有文件,并显示一条消息,说明该文件已存在。默认情况下,如果指定路径中存在文件,则“ Out-File”会覆盖文件而不会发出警告。

    是否必需?                    False
    位置?                        named
    默认值                False
    是否接受管道输入?            False
    是否接受通配符?              False

-NoNewline <System.Management.Automation.SwitchParameter>  (NoNewline-不换行)
  Specifies that the content written to the file does not end with a newline character. The string representations of the input objects are concatenated to form the output. No spaces or newlines are inserted between the output strings. No newline is added after the last output string.
   指定写入文件的内容不以换行符结尾。连接输入对象的字符串表示形式以形成输出。在输出字符串之间不插入空格或换行符。最后一个输出字符串后未添加换行符。

    是否必需?                    False
    位置?                        named
    默认值                False
    是否接受管道输入?            False
    是否接受通配符?              False

-Width <System.Int32> (Width-宽度)
    Specifies the number of characters in each line of output. Any additional characters are truncated, not wrapped. If this parameter is not used, the width is determined by the characteristics of the host. The default for the PowerShell console is 80 characters.
   指定输出的每一行中的字符数。任何其他字符都将被截断,而不是被包装。
   如果不使用此参数,则宽度由主机的特性确定,PowerShell控制台的默认值为80个字符。

    是否必需?                    False
    位置?                        named
    默认值                None
    是否接受管道输入?            False
    是否接受通配符?              False

-Confirm <System.Management.Automation.SwitchParameter> (Confirm-确认)
    Prompts you for confirmation before running the cmdlet.
    运行cmdlet之前提示您进行确认。

    是否必需?                    False
    位置?                        named
    默认值                False
    是否接受管道输入?            False
    是否接受通配符?              False

-WhatIf <System.Management.Automation.SwitchParameter> (WhatIf-假设)
    Shows what would happen if the cmdlet runs. The cmdlet is not run.
    显示如果cmdlet运行后将要发生的情况,该cmdlet没有运行。

    是否必需?                    False
    位置?                        named
    默认值                False
    是否接受管道输入?            False
    是否接受通配符?              False

<CommonParameters> 常见参数
    此 Cmdlet 支持常见参数: Verbose(冗长)、Debug、
    ErrorAction、ErrorVariable、WarningAction、WarningVariable、
    OutBuffer、PipelineVariable 和 OutVariable。有关详细信息,请参阅
    about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216)。

输入
System.Management.Automation.PSObject
You can pipe any object to Out-File.
您可以将任何对象通过管道传输到“文件外”。

输出
None
Out-File does not generate any output.
Out-File不产生任何输出。

注释

    The `Out` cmdlets do not format objects; they just render them and send them to the specified display destination. If you send an unformatted object to an `Out` cmdlet, the cmdlet sends it to a formatting cmdlet before rendering it.
  Out cmdlet不格式化对象;他们只是渲染它们并将它们发送到指定的显示目标。如果将未格式    化的对象发送到`Out` cmdlet,则该cmdlet会在呈现它之前将其发送到格式化cmdlet。

    To send a PowerShell command's output to the `Out-File` cmdlet, use the pipeline. You can store data in a variable and use the InputObject parameter to pass data to the `Out-File` cmdlet.
  要将PowerShell命令的输出发送到`Out-File` cmdlet,请使用管道。您可以将数据存储在变量

中,并使用InputObject参数将数据传递到Out-File cmdlet。

    `Out-File` sends data but it does not produce any output objects. If you pipe the output of `Out-File` to `Get-Member`, the `Get-Member` cmdlet reports that no objects were specified.
    Out-File发送数据,但不产生任何输出对象。如果将“ Out-File”的输出通过管道传递到“ Get-

Member”,则“ Get-Member`” cmdlet将报告未指定对象。

----------- Example 1: Send output and create a file -----------发送输出并创建文件

Get-Process | Out-File -FilePath .\Process.txt
Get-Content -Path .\Process.txt

NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
 ------    -----      -----     ------      --  -- -----------
     29    22.39      35.40      10.98   42764   9 Application
     53    99.04     113.96       0.00   32664   0 CcmExec
     27    96.62     112.43     113.00   17720   9 Code

The `Get-Process` cmdlet gets the list of processes running on the local computer. The Process objects are sent down the pipeline to the `Out-File` cmdlet. `Out-File` uses the FilePath parameter and creates a file in the current directory named Process.txt . The `Get-Content` command gets content from the file and displays it in the PowerShell console.

“ Get-Process” cmdlet获取在本地计算机上运行的进程的列表。 Process对象沿着管道发送到Out-File cmdlet。 Out-File使用FilePath参数并在当前目录中创建一个名为Process.txt的文件。 “Get-Content”命令从文件获取内容并将其显示在PowerShell控制台中。

-- Example 2: Prevent an existing file from being overwritten --防止现有文件被覆盖

Get-Process | Out-File -FilePath .\Process.txt -NoClobber

Out-File : The file 'C:\Test\Process.txt' already exists.
At line:1 char:15
+ Get-Process | Out-File -FilePath .\Process.txt -NoClobber
+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The `Get-Process` cmdlet gets the list of processes running on the local computer. The
Process objects are sent down the pipeline to the `Out-File` cmdlet. `Out-File` uses the FilePath parameter and attempts to write to a file in the current directory named Process.txt . The NoClobber parameter prevents the file from being overwritten and displays a message that the file already exists.

“ Get-Process” cmdlet获取在本地计算机上运行的进程的列表。 Process对象沿着管道发送到Out-File cmdlet。 Out-File使用FilePath参数并尝试写入名为Process.txt的当前目录中的文件。NoClobber参数可防止文件被覆盖并显示一条消息,说明该文件已存在。

------- Example 3: Send output to a file in ASCII format -------将输出发送到ASCII格式的文件

$Procs = Get-Process
Out-File -FilePath .\Process.txt -InputObject $Procs -Encoding ASCII -Width 50

The `Get-Process` cmdlet gets the list of processes running on the local computer. The Process objects are stored in the variable, `$Procs`. `Out-File` uses the FilePath parameter and creates a file in the current directory named Process.txt . The InputObject parameter passes the process objects in `$Procs` to the file Process.txt . The Encoding parameter converts the output to ASCII format. The Width parameter limits each line in the file to 50 characters so some data might be truncated.

“ Get-Process” cmdlet获取在本地计算机上运行的进程的列表。 Process对象存储在变量
$ Procs中。Out-File使用FilePath参数并在当前目录中创建一个名为Process.txt的文件。 InputObject参数将$ Procs中的过程对象传递到文件Process.txt。 Encoding参数将输出转换为ASCII格式。 Width参数将文件中的每一行限制为50个字符,因此某些数据可能会被截断。

----- Example 4: Use a provider and send output to a file -----使用提供程序并将输出发送到文件

PS> Set-Location -Path Alias:

PS> Get-Location

Path
----
Alias:\

PS> Get-ChildItem | Out-File -FilePath C:\TestDir\AliasNames.txt

PS> Get-Content -Path C:\TestDir\AliasNames.txt

CommandType     Name
-----------     ----
Alias           % -> ForEach-Object
Alias           ? -> Where-Object
Alias           ac -> Add-Content
Alias           cat -> Get-Content

The `Set-Location` command uses the Path parameter to set the current location to the registry provider `Alias:`. The `Get-Location` cmdlet displays the complete path for `Alias:`. `Get-ChildItem` sends objects down the pipeline to the `Out-File` cmdlet. `Out-File` uses the FilePath parameter to specify the complete path and filename for the output, C:\TestDir\AliasNames.txt . The `Get-Content` cmdlet uses the Path parameter and displays the file's content in the PowerShell console.

“ Set-Location”命令使用Path参数将当前位置设置为注册表提供程序“ Alias:”。
“ Get-Location” cmdlet显示“ Alias:”的完整路径。
Get-ChildItem将对象沿着管道发送到Out-File cmdlet。
Out-File使用FilePath参数指定输出C:\ TestDir \ AliasNames.txt的完整路径和文件名。
“ Get-Content” cmdlet使用Path参数并在PowerShell控制台中显示文件的内容。

相关链接
Online Version: https://docs.microsoft.com/powershell/module/microsoft.powershell.utili
ty/out-file?view=powershell-5.1&WT.mc_id=ps-gethelp
about_Providers
about_Quoting_Rules
Out-Default
Out-GridView
Out-Host
Out-Null
Out-Printer
Out-String
Tee-Object

posted @ 2020-11-12 15:11  DuoReMin  阅读(164)  评论(0)    收藏  举报