- PowerShell功能特点
- PowerShell历史
- PowerShell和命令提示符的区别
- PowerShell与Bash Shell的区别
- PowerShell以管理员身份运行
- Windows PowerShell ISE
- PowerShell核心
- 创建并运行PowerShell脚本
- PowerShell注释
- PowerShell Cmdlet
- PowerShell基本cmdlet命令
- PowerShell Get-childItem命令
- PowerShell Get-Item命令
- PowerShell Get-Location命令
- PowerShell set-item命令
- PowerShell set-location命令
- PowerShell new-item命令
- PowerShell copy-item命令
- PowerShell move-item命令
- PowerShell remove-item命令
- PowerShell rename-item命令
- PowerShell add-content命令
- PowerShell clear-content
- PowerShell get-content命令
- PowerShell get-date命令
- PowerShell set-content命令
- PowerShell out-file命令
- PowerShell write-host命令
- PowerShell get-command命令
- PowerShell invoke-command命令
- PowerShell get-help命令
- PowerShell start-process命令
- PowerShell test-path命令
- PowerShell foreach-object命令
- PowerShell sort-object命令
- PowerShell where-object命令
- PowerShell变量
- PowerShell自动变量
- PowerShell首选项变量
- PowerShell数组
- PowerShell哈希表
- PowerShell运算符
- PowerShell算术运算符
- PowerShell赋值运算符
- PowerShell比较运算符
- PowerShell逻辑运算符
- PowerShell重定向运算符
- PowerShell拆分和合并运算符
- PowerShell if语句
- PowerShell if-else语句
- PowerShell else-if语句
- PowerShell Switch语句
- PowerShell Do-While循环
- PowerShell for循环
- PowerShell ForEach循环
- PowerShell While循环
- PowerShell Continue和Break
- PowerShell字符串
- PowerShell函数
- PowerShell Try Catch Finally
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>
本示例创建一个新文件,并将现有文件的内容复制到新文件中。
扫描二维码
程序员编程王