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

[玩转系统] 添加内容 - 在 PowerShell 中将文本附加到文件

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

添加内容 - 在 PowerShell 中将文本附加到文件


在 PowerShell 中处理文件时,我们必须使用 PowerShell 将文本附加到文件,例如将文本添加到文件末尾、将字符串附加到文件、添加来自不同文件的内容或将 DateTime 附加到文件末尾。

PowerShell 中的 Add-Content cmdlet 向文件添加内容,您可以使用命令中的内容指定内容,也可以使用 Get-Content 获取文件内容以进行追加。

使用 Add-Content cmdlet,它将文本附加到文件或将字符串附加到文件。

在这篇博文中,我将通过不同的示例解释 PowerShell Add-Content,以将内容附加到文件。

让我们练习一下 PowerShell 添加内容示例。

添加内容语法

PowerShell 中的 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>]

使用添加内容将文本附加到文件

让我们考虑一个例子,目录中有 Get-DateFile.txt 文件。

让我们使用 PowerShell 将数据附加到文本文件,创建一个名为 Get-DateFile.txt 的新文件,并向其中添加一些数据。

[玩转系统] 添加内容 - 在 PowerShell 中将文本附加到文件

Add-Content -Path .\Get-DateFile.txt "End of file"

在上面的示例中,Add-Content 将“End of file”文本数据附加到当前目录中 Path 参数指定的文件末尾。

将数据添加到文件后的输出是:

[玩转系统] 添加内容 - 在 PowerShell 中将文本附加到文件

将数据附加到新行的文本文件

要将数据追加到文本文件的新行中,请使用“n”(PowerShell 新行)。

考虑上面的示例,要将文本添加到新行的文件中,请使用

 Add-Content -Path .\Get-DateFile.txt "`nSecond para starts here..." 

在上面的 PowerShell 脚本中,我们使用 `n 在文本前添加 PowerShell 新行。它将在新行上写入一行到文件。

上述脚本的输出是:

[玩转系统] 添加内容 - 在 PowerShell 中将文本附加到文件

将日期时间添加到文件末尾

使用 PowerShell 中的 Add-Content cmdlet,您可以将 DateTime 添加到文件末尾,如下所示

 Add-Content -Path .\Get-DateFile.txt -Value (Get-Date) 

在上面的示例中,Add-Content 将日期时间附加到 Path 参数指定的文本文件中。 Value 参数指定写入文件的字符串。

酷提示:如何使用 PowerShell Copy-Item 在 PowerShell 中复制文件!

将一个文件的内容添加到另一个文件

如果要将一个文件的内容附加到另一个文件,可以使用 Add-Content cmdlet 轻松完成。

它将读取文件内容到变量并使用 Add-Content 将内容写入目标文件。

此示例演示如何使用 PowerShell cmdlet 从一个文件获取内容并将其附加到另一文件。

如果在将文本添加到文件时文件不存在,Add-Content 将创建一个新文件。

# Read file contents to variable
$sourceFileContent = Get-Content -Path .\GetFileProperties-Assignment.txt 

# Appends contents of one file to another file
# If the file does not exist, Add-Content create a new file
Add-Content -Path .\DestinationFile.txt -Value $sourceFileContent 

在上面的示例中,将一个文件的内容附加到另一个文件,

PowerShell 中的 Get-Content cmdlet 获取由 Path 参数指定的文件内容。它读取文件内容并将其存储在变量 $sourceFileContent 中。

PowerShell 中的 Add-Content cmdlet 会附加 Value 参数中指定的源文件的内容。

如果文件不存在,Add-Content cmdlet 将创建一个新文件并复制内容。

将数据追加到只读文件

使用 PowerShell 中的 Add-Content cmdlet,它还将内容附加到只读文件中。

让我们了解如何使用示例将数据附加到只读文件......

# Create new file
New-Item -Path .\TempReadonlyFile.txt -ItemType File

# Set file as read only
Set-ItemProperty -Path .\TempReadonlyFile.txt -Name IsReadOnly -Value $True 

# Get file details
Get-ChildItem -Path .\TempReadonlyFile.txt  

# Appends the line to file
Add-Content -Path .\TempReadonlyFile.txt -Value 'End of File' -Force

在上面的示例中,将一行追加到文件中,

第一个命令使用 PowerShell 中的 New-Item cmdlet 创建一个新的空文件。

PowerShell 中的 Set-ItemProperty cmdlet 将指定文件的 IsReadOnly 属性设置为 true

PowerShell 中的 Get-ChildItem cmdlet 获取指定的文件详细信息,如模式、LastWriteTime、长度和名称

Add-Content cmdlet 将一行追加到由 Path 参数指定的只读文件中。

将一行附加到只读文件的上述脚本的输出是:

[玩转系统] 添加内容 - 在 PowerShell 中将文本附加到文件

酷提示:如何使用 Clear-Content 在 PowerShell 中清除文本文件中的内容!

结论

我希望上面关于添加内容以将文本附加到文件的博客文章对您有所帮助。

PowerShell 中的 Add-Content cmdlet 将另一个文件中的多行附加到源文件,将 DateTime 添加到文件末尾,并将文本附加到只读文件。

您可以在 ShellGeek 主页上找到有关 PowerShell Active Directory 命令和 PowerShell 基础知识的更多主题。

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

取消回复欢迎 发表评论:

关灯