[玩转系统] 使用 PowerShell 将数据附加到文件 [ 4 种方法]
作者:精品下载站 日期:2024-12-14 05:29:57 浏览:15 分类:玩电脑
使用 PowerShell 将数据附加到文件 [ 4 种方法]
将文本附加到文本文件
我们有多种方法使用 PowerShell 在文本文件中附加内容。例如,我们可以在同一行、带/不带特殊字符的新行、多行上附加信息,以及附加格式化数据(例如表格内容)。但是,在转向可能的解决方案之前,必须了解我们将在本节下面的每种方法中使用的文件和文件内容。 file.txt
位于 E:\Test 目录中。
file.txt文件的内容:
This is the first line.
请注意,我们的光标位于同一行,就在句点 (.
) 之后,这将帮助我们在同一行上追加内容。让我们使用这个文件来探索下面的各种解决方案。
使用 Out-File
Cmdlet
使用 Out-File
cmdlet 和 -Append
参数将内容附加到 file.txt
文件中的同一行。
使用输出文件 Cmdlet:
$filePath = "E:\Test\file.txt"
$content = "This is the second line."
Out-File -FilePath $filePath -InputObject $content -Append
Get-Content $filePath
输出 :
This is the first line.This is the second line.
使用 Out-File
cmdlet 和 -Append
参数将内容附加到 file.txt
文件的新行上。
使用输出文件 Cmdlet:
$filePath = "E:\Test\file.txt"
$content = "This is the third line."
Out-File -FilePath $filePath -InputObject $content -Append
Get-Content $filePath
输出 :
This is the first line.This is the second line.
This is the third line.
你发现上面两个例子有什么相似之处吗?是的!为什么会这样呢?因为在第一个示例中第一次使用了 -Append
,所以它将内容附加到与光标位于同一行的同一行上,但也在文件末尾添加了一个新行字符。现在,光标已换行;这就是为什么第二个代码示例中的 -Append
在新行中添加了内容,但它还在文件末尾添加了一个新行字符。请注意,-Append
参数会自动在文件末尾添加换行符;这就是为什么除了第一个示例之外每次都会将内容附加到新行的原因。
使用带有 -Append
参数的 Out-File
cmdlet 在 file.txt
中附加带有特殊字符(例如制表符)的内容> 文件。
使用输出文件 Cmdlet:
$filePath = "E:\Test\file.txt"
$content = "`tThis fourth line is with tab character."
Out-File -FilePath $filePath -InputObject $content -Append
Get-Content $filePath
输出 :
This is the first line.This is the second line.
This is the third line.
This fourth line is with tab character.
使用 Out-File
cmdlet 和 -Append
参数在 file.txt
文件中追加多行。
使用输出文件 Cmdlet:
$filePath = "E:\Test\file.txt"
$content = @("This is the fifth line.",
"This is the sixth line.",
"This is the seventh line.")
Out-File -FilePath $filePath -InputObject $content -Append
Get-Content $filePath
输出 :
This is the first line.This is the second line.
This is the third line.
This fourth line is with tab character.
This is the fifth line.
This is the sixth line.
This is the seventh line.
使用 Out-File
cmdlet 和 -Append
参数在 file.txt
文件中附加表格内容(格式化内容)。
使用输出文件 Cmdlet:
$filePath = "E:\Test\file.txt"
$content = @("First`tLast`tAge",
"John`tWilliamson`t50",
"Mary`tPurell`t30",
"Sam`tAnderson`t35")
Out-File -FilePath $filePath -InputObject $content -Append
Get-Content $filePath
输出 :
This is the first line.This is the second line.
This is the third line.
This fourth line is with tab character.
This is the fifth line.
This is the sixth line.
This is the seventh line.
First Last Age
John Williamson 50
Mary Purell 30
Sam Anderson 35
使用 Out-File
cmdlet 在 file.txt
文件中附加包含单引号和双引号的行。
使用输出文件 Cmdlet:
$filePath = "E:\Test\file.txt"
$content = "This is the eighth line having `'single quote`' and `"double quotes`"."
Out-File -FilePath $filePath -InputObject $content -Append
Get-Content $filePath
输出 :
This is the first line.This is the second line.
This is the third line.
This fourth line is with tab character.
This is the fifth line.
This is the sixth line.
This is the seventh line.
First Last Age
John Williamson 50
Mary Purell 30
Sam Anderson 35
This is the eighth line having 'single quote' and "double quotes".
Out-File cmdlet 用于将输出发送到指定文件。此 cmdlet 隐式使用 PowerShell 的格式化系统写入文件。请注意,此文件将获得与终端上完全相同的显示表示形式,这意味着如果输出中涉及的每个输入对象都是字符串,则输出可能足以进行编程处理。我们使用此 cmdlet 将输出 ($content
) 发送到 file.txt
文件。
在上面的示例中,我们创建了两个名为 $filePath
和 $content
的变量。 $filePath
包含 file.txt
文件的路径,而 $content
包含我们要附加的实际内容。 Get-Content
cmdlet 用于检索 file.txt
文件的全部数据(内容)并将其显示在终端上,这有助于查看更新的内容无需打开 file.txt
文件。
唯一的区别是我们如何根据需求使用 Out-File
cmdlet。所有示例中的 -FilePath
、-InputObject
和 -Append
参数均相同。那么这些参数的含义是什么,我们为什么要使用它们呢?
-FilePath
用于指定 $filePath
,-InputObject
用于提及我们想要的 $content
追加,-Append
用于将 $content
追加到 file.txt
文件的末尾。 您还可以使用 -Encoding
参数指定目标文件的编码类型。通常,使用 UTF8
编码,而 utf8NoBOM
为其默认值。我们还有其他编码,可以根据我们的要求使用。
现在,重点是内容的格式如何。这是在初始化每个示例的 $content
变量时完成的。我们使用简单的文本在第一个和第二个代码片段中设置 $content
变量。在第三个示例中,我们使用前面带有 `t(特殊字符)的文本来初始化 $content
,以将文本附加到 tab
后面。
类似地,在第四个示例中,我们使用 @()
表示的数组运算符来创建一个字符串数组,其中每个字符串都是我们一次附加到文件中的一行文本。最后,在第五个示例中,我们使用带有特殊字符的@()
来以表格形式附加数据;这些特殊字符包括换行符和制表符。您可以在下面找到不同的特殊字符及其描述。
描述
`0无效的
`a警报
`b退格键
`n换行符
`r回车
`t水平制表符
`'单引号
`"双引号
在本节的第七个也是最后一个示例中,我们在初始化 $content
的值时使用了单引号和双引号。请注意,每个引号(单引号和双引号)前面都有一个反引号,如上表所示。
如果该文件(在本例中为 file.txt
)不存在,则 Out-File
cmdlet 将创建该文件并执行其余操作。
使用 Add-Content
Cmdlet
使用 Add-Content
cmdlet 将内容附加到 file.txt
文件中的同一行。
使用添加内容 Cmdlet:
$filePath = "E:\Test\file.txt"
$content = "This is the second line."
Add-Content -Path $filePath -value $content
Get-Content $filePath
输出 :
This is the first line.This is the second line.
使用 Add-Content
cmdlet 将内容附加到 file.txt
文件中的新行。
使用添加内容 Cmdlet:
$filePath = "E:\Test\file.txt"
$content = "This is the third line."
Add-Content -Path $filePath -value $content
Get-Content $filePath
输出 :
This is the first line.This is the second line.
This is the third line.
使用 Add-Content
cmdlet 在 file.txt
文件中附加带有特殊字符(例如制表符)的内容。
使用添加内容 Cmdlet:
$filePath = "E:\Test\file.txt"
$content = "`tThis fourth line is with tab character."
Add-Content -Path $filePath -value $content
Get-Content $filePath
输出 :
This is the first line.This is the second line.
This is the third line.
This fourth line is with tab character.
使用 Add-Content
cmdlet 在 file.txt
文件中附加多行。
使用添加内容 Cmdlet:
$filePath = "E:\Test\file.txt"
$content = @("This is the fifth line.",
"This is the sixth line.",
"This is the seventh line.")
Add-Content -Path $filePath -value $content
Get-Content $filePath
输出 :
This is the first line.This is the second line.
This is the third line.
This fourth line is with tab character.
This is the fifth line.
This is the sixth line.
This is the seventh line.
使用 Add-Content
cmdlet 在 file.txt
文件中附加表格内容(格式化内容)。
使用添加内容 Cmdlet:
$filePath = "E:\Test\file.txt"
$content = @("First`tLast`tAge",
"John`tWilliamson`t50",
"Mary`tPurell`t30",
"Sam`tAnderson`t35")
Add-Content -Path $filePath -value $content
Get-Content $filePath
输出 :
This is the first line.This is the second line.
This is the third line.
This fourth line is with tab character.
This is the fifth line.
This is the sixth line.
This is the seventh line.
First Last Age
John Williamson 50
Mary Purell 30
Sam Anderson 35
使用 Add-Content
cmdlet 在 file.txt
文件中附加包含单引号和双引号的行。
使用添加内容 Cmdlet:
$filePath = "E:\Test\file.txt"
$content = "This is the eighth line having `'single quote`' and `"double qoutes`"."
Add-Content -Path $filePath -value $content
Get-Content $filePath
输出 :
This is the first line.This is the second line.
This is the third line.
This fourth line is with tab character.
This is the fifth line.
This is the sixth line.
This is the seventh line.
First Last Age
John Williamson 50
Mary Purell 30
Sam Anderson 35
This is the eighth line having 'single quote' and "double quotes".
我们使用 Add-Content 将内容添加到给定文件。我们可以通过直接在命令中指定文本来使用此 cmdlet,或者将其存储在对象中并在使用 Add-Content
cmdlet 时使用该对象。例如,我们将内容存储在 $content
变量中,并在使用此 cmdlet 时使用该变量;请参阅上面的代码围栏。
本节的示例与使用 Out-File
cmdlet 的示例类似。再次,我们定义并初始化了 $filePath
和 $content
以包含文件路径和内容。 Get-Content
用于从 file.txt
文件获取内容并将其显示在终端上。存储在 $content
变量中的内容的格式与我们在上一节中使用 Out-File
cmdlet 时所做的完全一样;你可以参考一下。
在本节的所有示例中,我们使用 -Path
和 -value
参数以及 Add-Content
cmdlet 来指定 $filePath
和 $content
。
如果文本文件尚不存在,Add-Content
将创建具有指定名称的文本文件。如果未给出路径,则将在当前目录中创建文本文件。
使用重定向 (>>
) 运算符
使用 >>
运算符将内容附加到 file.txt
文件中的同一行。
使用重定向运算符:
$filePath = "E:\Test\file.txt"
$content = "This is the second line."
$content>>$filePath
Get-Content $filePath
输出 :
This is the first line.This is the second line.
使用 >>
运算符将内容附加到 file.txt
文件中的新行。
使用重定向运算符:
$filePath = "E:\Test\file.txt"
$content = "This is the third line."
$content>>$filePath
Get-Content $filePath
输出 :
This is the first line.This is the second line.
This is the third line.
使用 >>
运算符在 file.txt
文件中附加带有特殊字符(例如制表符)的内容。
使用重定向运算符:
$filePath = "E:\Test\file.txt"
$content = "`tThis fourth line is with tab character."
$content>>$filePath
Get-Content $filePath
输出 :
This is the first line.This is the second line.
This is the third line.
This fourth line is with tab character.
使用>>
cmdlet 在file.txt
文件中附加多行。
使用重定向运算符:
$filePath = "E:\Test\file.txt"
$content = @("This is the fifth line.",
"This is the sixth line.",
"This is the seventh line.")
$content>>$filePath
Get-Content $filePath
输出 :
This is the first line.This is the second line.
This is the third line.
This fourth line is with tab character.
This is the fifth line.
This is the sixth line.
This is the seventh line.
使用 >>
运算符在 file.txt
文件中追加表格内容(格式化内容)。
使用添加内容 Cmdlet:
$filePath = "E:\Test\file.txt"
$content = @("First`tLast`tAge",
"John`tWilliamson`t50",
"Mary`tPurell`t30",
"Sam`tAnderson`t35")
$content>>$filePath
Get-Content $filePath
输出 :
This is the first line.This is the second line.
This is the third line.
This fourth line is with tab character.
This is the fifth line.
This is the sixth line.
This is the seventh line.
First Last Age
John Williamson 50
Mary Purell 30
Sam Anderson 35
使用 >>
运算符在 file.txt
文件中附加包含单引号和双引号的行。
使用添加内容 Cmdlet:
$filePath = "E:\Test\file.txt"
$content = "This is the seventh line having `'single quote`' and `"double quotes`"."
$content>>$filePath
Get-Content $filePath
输出 :
This is the first line.This is the second line.
This is the third line.
This fourth line is with tab character.
This is the fifth line.
This is the sixth line.
This is the seventh line.
First Last Age
John Williamson 50
Mary Purell 30
Sam Anderson 35
This is the seventh line having 'single quote' and "double quotes".
这些示例、cmdlet 和变量与前面部分中的相同,但我们使用重定向运算符来附加 file.txt
文件。
如果文本文件尚不存在,则重定向运算符将在指定目录(如果未给出,则在当前目录)中创建文本文件。
将一个文本文件附加到另一个文本文件
使用 Add-Content
将一个文本文件的内容附加到 PowerShell 中的另一个文本文件中。
file1.txt文件的内容:
This is the file1 line.
file2.txt文件的内容:
This is the file2 line.
使用添加内容 Cmdlet:
$sourceFile = "E:\Test\file2.txt"
$destinationFile = "E:\Test\file1.txt"
$sourceFileContent = Get-Content -Path $sourceFile
Add-Content -Path $destinationFile -value $sourceFileContent
Get-Content $destinationFile
输出:
This is the file1 line.This is the file2 line.
这就是如何使用 PowerShell 将数据附加到文件的全部内容。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag