PowerShell add-content命令

Add-Content cmdlet将内容附加到指定的项目或文件,例如将单词,文本,字符等添加到文件。 我们可以通过在cmdlet中键入内容或指定包含内容的对象来指定内容。

语法

语法1

Add-Content   
[-Path <string[]>]   
[-Credential <pscredential>]   
[-WhatIf]   
[-Confirm]  
[-Value <Object[]>]   
[-PassThru]   
[-Filter <string>]   
[-Include <string[]>]   
[-Exclude <string[]>]   
[-Force]   
[-UseTransaction]   
[-NoNewline]   
[-Encoding {Unknown | String | Unicode | Byte | BigEndianUnicode | UTF8 | UTF7 | UTF32 | Ascii | Default | Oem |BigEndianUTF32}]   
[-Stream <string>]    
[<CommonParameters>]

语法2

Add-Content   
[-Value <Object[]>]  
[ -LiteralPath <string[]>]  
[-Credential <pscredential>]   
[-WhatIf]   
[-Confirm]  
[-PassThru]   
[-Filter <string>]   
[-Include <string[]>]  
[-Exclude <string[]>]   
[-Force]   
[-UseTransaction]   
[-NoNewline]  
[-Encoding {Unknown | String | Unicode | Byte | BigEndianUnicode | UTF8 | UTF7 | UTF32 | Ascii | Default | Oem |BigEndianUTF32}]   
[-Stream <string>]    
[<CommonParameters>]

参数

-Path - 此参数用于指定项目当前位置的路径,接受通配符。 默认情况下,其值为当前目录。
-LiteralPath - 此参数用于指定位置的路径。 它的值与键入时完全一样。 如果路径包含转义符,则将其用单引号引起来。单引号告诉Windows PowerShell,它不应将任何字符解释为转义序列。
-Destination - 此参数用于指定复制项目的位置的路径。 默认情况下,其值为当前目录。 可以使用通配符,但是输出必须指定一个位置。
-Destination - 此参数的值中指定一个新名称,以重命名要移动的项目。
-Force - 此参数用于强制执行命令而不要求用户确认。
-Filter - 此参数指定用于限定-Path参数的过滤器。 FileSystem提供程序是唯一支持使用过滤器的PowerShell提供程序。 此参数效率更高,因为提供程序在cmdlet获取对象时应用筛选器,而不是让Powershell在访问对象后筛选对象。
-Include- 此cmdlet包括在操作中的项目被指定为字符串数组。 -Include参数的值限定-Path参数。 输入模式或路径元素,例如*.txt。 仅当cmdlet包含项目的内容(例如C:\*,通配符*用于指定C:目录的内容)时,-Include参数才有效。
-Exclude - 此cmdlet在操作中排除的项目指定为字符串数组。 -Exclude参数的值限定-Path参数。 输入模式或路径元素,例如*.txt。它接受通配符。 仅当cmdlet包含项目的内容(例如C:\*,通配符*用于指定C:目录的内容)时,-Exclude参数才有效。
-PassThru - 此参数返回一个对象,该对象表示正在使用的项目。 默认它不会产生任何输出。
-WhatIf - 此参数显示执行cmdlet将发生的情况。
-Confirm - 此参数在运行cmdlet之前提示确认。
-Value - 此参数用于指定要添加的内容。不能通过输入文件的路径来指定文件的内容,因为路径只是一个字符串。可以使用Get-content来获取内容并将其传递给-Value参数。
-Encoding - 此参数用于指定目标文件的编码类型。默认情况下,其值为UTF8NoBOM。 它是一个动态参数,FileSystem提供程序将其添加到Add-Content-Encoding参数仅在文件系统驱动器中起作用。以下是此参数可接受的值:ASCII,OEM,UTF8,UTF8BOM,UTF8NoBOM,Unicode,BigEndianUnicode,UTF32,UTF7
-NoNewLine - 此参数指示此cmdlet不会向内容添加新行或回车。 在输出字符串之间不会插入任何换行符或空格,并且在最后一个输出字符串之后也不会添加任何换行符。
-Stream - 此参数用于为内容指定备用数据流。如果流不存在,它将创建它。参数接受通配符。

示例

示例1: 将字符串添加到指定文件

PS E:\xntutor\powershell> add-content newfile.txt -value "这是一个测试添加内容"
PS E:\xntutor\powershell>

在此示例中,将内容附加到当前目录中的指定文件。

示例2: 将字符串添加到所有文本文件

PS E:\xntutor\powershell> add-content .\*.txt -value "这是一个测试添加内容(多个文件)"
PS E:\xntutor\powershell>

在此示例中,将内容附加到当前目录中的所有文本文件。

示例3: 在指定文件的末尾添加一个日期

PS E:\xntutor\powershell> add-content newfile2.txt -value (get-date)
PS E:\xntutor\powershell>

在此示例中,将日期值附加到当前目录中的指定文本文件。

示例4: 将指定文件的内容添加到另一个文件

PS E:\xntutor\powershell> $get = get-content file1.txt
PS E:\xntutor\powershell> add-content file2.txt -value $get
PS E:\xntutor\powershell> get-content file2.txt
The Add-Content cmdlet appends the content to the specified item or a file, such as adding words to the file.
We can specify the content by typing it in the cmdlet or by specifying an object which contains the content.
2020/1/31
PS E:\xntutor\powershell>

本示例从文件中获取内容并将其存储在变量中。此变量用于将内容附加到另一个文件中。Get-Content 接收file1.txt的内容,并将其存储在$get变量中。
Add-Content 使用$get变量的内容更新file2.txt文件。 Get-Content显示file2.txt

示例5: 创建一个新文件并复制内容

PS E:\xntutor\powershell> add-content newfile2.txt -value (get-content file2.txt)
PS E:\xntutor\powershell> get-content file2.txt
The Add-Content cmdlet appends the content to the specified item or a file, such as adding words to the file.
We can specify the content by typing it in the cmdlet or by specifying an object which contains the content.
2020/1/31
PS E:\xntutor\powershell>

本示例创建一个新文件,并将现有文件的内容复制到新文件中。


上一篇:PowerShell rename-item命令

下一篇:PowerShell clear-content

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程