【译】PowerShell Append to File的指南

311 阅读4分钟

PowerShell Append to File

PowerShell Append to File简介

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

PowerShell追加到文件的语法

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

1.Add-Content 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.Out-File 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 Append to File是如何工作的?

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

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

代码。

Get-Content C:\Temp\test.txt

输出。

PowerShell Append to File 1

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

代码。

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

输出。

PowerShell Append to File 2

最后一行被追加到新的一行。与其把它追加到最后一行,不如添加新的一行,使用回车(`n)。

代码。

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

输出结果。

PowerShell Append to File 3

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

代码。

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

输出。

PowerShell Append to File 4

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

PowerShell Append to File 5

还有其他方法,如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 Append to File 6

使用一个变量添加多行内容。

代码。

$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 Append to File 7

例子 #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

输出。

using Here-String command

例子 #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

输出。

using Out-File command

在这里,你会得到类似于>>的输出,因为编码的问题,当你用Notepad++编辑器检查输出文件时,它们被$null值填满。我们不希望这样,所以相反,我们可以使用这个cmdlet支持的编码方法,使用-encoding参数。

你可以使用标准的ASCII或utf8编码标准来获得适当的输出格式。

编码。

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

输出。

PowerShell Append to File 10

请确保使用-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

输出。

Set-Content command

例子 #5

追加CSV文件的数据。

如果你已经有了CSV文件,那么使用带有-Append参数的Export-CSV cmdlet可以很容易地将其追加到CSV文件中。

我们在C:\temp\Services.csv有以下的CSV文件,我们想把数据追加到它。

CSV file data

如果你想追加更多的服务,例如WinRM服务信息,那么你可以使用下面的命令。

代码。

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

输出。

WinRM service information

结论

向文件添加内容或附加文件的操作是最有用和最常用的操作之一,但手动做这些事情并将数据提供给最终用户,如财务、人力资源、法律部门等,是一项痛苦的任务,但使用这些cmdlets,我们也可以自动化和安排文件更新任务,无需任何用户干预。

推荐文章

这是一个关于PowerShell Append to File的指南。在这里我们讨论了介绍;PowerShell追加到文件是如何工作的?和例子。你也可以看看下面的文章来了解更多

  1. PowerShell睡眠
  2. PowerShell SubString
  3. PowerShell不喜欢
  4. PowerShell中的Else If

The postPowerShell Append to Fileappeared first onEDUCBA.