当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 附加到文件 | PowerShell 附加到文件如何工作?

作者:精品下载站 日期:2024-12-14 05:02:56 浏览:16 分类:玩电脑

PowerShell 附加到文件 | PowerShell 附加到文件如何工作?


[玩转系统] PowerShell 附加到文件 | PowerShell 附加到文件如何工作?

PowerShell 附加到文件简介

以下文章提供了 PowerShell 追加到文件的概述。 PowerShell 附加到文件操作是通过使用 Out-File、Add-Content、Export-CSV 等各种 cmdlet 将内容添加到不同类型的文件(如 TXT、CSV、Excel、JSON 等)的方法。以及现有文件上的各种方法,通过向内容添加新行或将数据附加到文件最后一行的延续部分。

PowerShell 附加到文件的语法

有多种 cmdlet 用于将内容附加到现有文件,但它们都不是直接的 cmdlet 或语法,而是使用有助于附加文件的参数。

1.添加内容 cmdlet。

Add-Content
[-Path] <string[]>
[-Value] <Object[]>
[-PassThru]
[-Filter <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Force]
[-Credential <pscredential>]
[-WhatIf]
[-Confirm]
[-NoNewline]
[-Encoding <Encoding>]
[-AsByteStream]
[-Stream <string>]
[<CommonParameters>]

2.输出文件 cmdlet。

Out-File
[-FilePath] <string>
[[-Encoding] <Encoding>]
[-Append]
[-Force]
[-NoClobber]
[-Width <int>]
[-NoNewline]
[-InputObject <psobject>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]

还有其他文件,例如 CSV,它有一个单独的 cmdlet 将内容附加到文件中。

Export-Csv
-InputObject <PSObject>
[[-Path] <String>]
[-LiteralPath <String>]
[-Force]
[-NoClobber]
[-Encoding <Encoding>]
[-Append]
[[-Delimiter] <Char>]
[-IncludeTypeInformation]
[-NoTypeInformation]
[-QuoteFields <String[]>]
[-UseQuotes <QuoteKind>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]

我们可以使用 Out-File 和 Export-CSV cmdlet 中的 -Append 参数将内容附加到文件中。

PowerShell 附加到文件如何工作?

使用 PowerShell 或其他编程语言附加文件或将内容添加到文件中并不是那么困难。在 PowerShell 中,支持使用各种 cmdlet 及其参数将内容附加到现有文件。

我们有一个名为 test.txt 的文件存储在 C:\Temp 中,其内容如下。

代码:

Get-Content C:\Temp\test.txt

输出:

[玩转系统] PowerShell 附加到文件 | PowerShell 附加到文件如何工作?

为了附加该行,我们将使用双箭头 (>>),这是基本语法,也是 .Net 方法中最常见的方法。

代码:

"This is the fourth line" >> C:\Temp\test.txt
Get-Content C:\Temp\test.txt

输出:

[玩转系统] PowerShell 附加到文件 | PowerShell 附加到文件如何工作?

最后一行被附加到新行中。不要将其附加到最后一行,而是添加新行,并使用回车符 (`n)。

代码:

"`nThis is the fourth line" >> C:\Temp\test.txt
Get-Content C:\Temp\test.txt

输出:

[玩转系统] PowerShell 附加到文件 | PowerShell 附加到文件如何工作?

如果要添加多行,可以直接使用append,也可以使用字符串变量。

代码:

$str = "`nThis is the fourth line.`nThis is the 5th line"
$str >> C:\Temp\test.txt
Get-Content C:\Temp\test.txt

输出:

[玩转系统] PowerShell 附加到文件 | PowerShell 附加到文件如何工作?

但不推荐这种方法,因为当你在其他编辑器(使用notepad++)中检查该文件时,你可以看到添加了空值。下面的快照来自 Notepad++ 编辑器。

[玩转系统] PowerShell 附加到文件 | PowerShell 附加到文件如何工作?

还有其他方法,如 Out-File、Add-Content 等,可以附加内容,如下面的示例所示。

PowerShell 附加到文件的示例

下面给出了 PowerShell 追加到文件的示例:

例子#1

使用 Add-Content cmdlet 附加该文件。

我们将使用同一文件 test.txt 通过 Add-Content cmdlet 附加该文件的内容。

代码:

"This is the 4th line" | Add-Content -Path C:\Temp\test.txt

或者

Add-Content -Value "This is the 4th line" -Path C:\Temp\test.txt
Get-Content C:\Temp\test.txt

输出:

[玩转系统] PowerShell 附加到文件 | PowerShell 附加到文件如何工作?

使用变量添加多行。

代码:

$str = "`nThis is the 4th line. `nThis is the 5th line. `nThis is the 6th line"
$str | Add-Content -Path C:\Temp\test.txt

或者

Add-Content -Value $str -Path C:\Temp\test.txt
Get-Content C:\Temp\test.txt

输出:

[玩转系统] PowerShell 附加到文件 | PowerShell 附加到文件如何工作?

例子#2

使用 Here-String 命令附加内容。

使用 here-string @ ”.. ”@ 方法附加多行字符串的最佳方法如下所示。

代码:

$str = @"
`nThis is the 4th line
This is the 5th line
This is the 6th line
"@
$str | Add-Content C:\Temp\test.txt
Get-Content C:\Temp\test.txt

输出:

[玩转系统] PowerShell 附加到文件 | PowerShell 附加到文件如何工作?

例子#3

使用 Out-File 命令附加文件。

我们可以使用带有-Append参数的Out-File命令将内容添加到文件中。

代码:

$str = @"
`nThis is the 4th line
This is the 5th line
This is the 6th line
"@
$str | Out-File -FilePath C:\Temp\test.txt -Append -Force
Get-Content C:\Temp\test.txt

输出:

[玩转系统] PowerShell 附加到文件 | PowerShell 附加到文件如何工作?

在这里,由于编码问题,您将得到类似于 >> 输出的输出,并且当您使用 Notepad++ 编辑器检查输出文件时,它们被 $null 值填充。我们不希望这样,因此我们可以使用 -encoding 参数来使用此 cmdlet 支持的编码方法。

您可以使用标准 ASCII 或 utf8 编码标准来获得正确的输出格式。

代码:

$str | Out-File -FilePath C:\Temp\test.txt -Append -Encoding ascii -Force

输出:

[玩转系统] PowerShell 附加到文件 | PowerShell 附加到文件如何工作?

确保使用-Append参数;否则,该文件将被覆盖。

例子#4

使用 Set-Content 命令添加内容。

我们还可以使用Set-Content命令来追加文件,但这不是文件追加操作的标准命令,因为我们需要在现有文件中添加新内容后覆盖整个内容。

代码:

$str = @"
This is the 4th line
This is the 5th line
This is the 6th line
"@
$Inputfile = 'C:\Temp\test.txt'
$file = Get-Content $Inputfile
$file + $str | Set-Content $Inputfile -Force
Get-Content $Inputfile

输出:

[玩转系统] PowerShell 附加到文件 | PowerShell 附加到文件如何工作?

例子#5

附加 CSV 文件数据。

如果您已有 CSV 文件,则可以使用带有 -Append 参数的 Export-CSV cmdlet 轻松将其附加到 CSV 文件。

我们在 C:\temp\Services.csv 中存在以下 CSV 文件,我们希望将数据附加到其中。

[玩转系统] PowerShell 附加到文件 | PowerShell 附加到文件如何工作?

如果您想附加更多服务,比如 WinRM 服务信息,那么您可以使用以下命令。

代码:

Get-Service Winrm | Select Name, DisplayName, Status, StartType | Export-Csv C:\Temp\services.csv -Append -Force

输出:

[玩转系统] PowerShell 附加到文件 | PowerShell 附加到文件如何工作?

结论

将内容添加到文件或附加文件操作是最有用和最常用的操作之一,但手动完成所有这些工作并向财务、人力资源、法律部门等最终用户提供数据是一项痛苦的任务。但使用这些 cmdlet,我们还可以自动执行和安排文件更新任务,而无需任何用户干预。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯