- 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 set-content命令
PowerShell Set-Content
用于将新内容写入或替换文件中已经存在的内容。 它不同于Add-content
cmdlet,Add-content
将内容附加到文件。 可以在Set-Content
cmdlet命令行上使用-Value
参数将内容发送到文件,也可以通过管道发送内容。 sc
是此cmdlet的别名。
语法
示例1
Set-Content [-Path] <string[]> [-Force] [-Credential <pscredential>] [-WhatIf] [-Confirm] [-Value] <Object[]> [-PassThru] [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-UseTransaction] [-NoNewline] [-Encoding {Unknown | String | Unicode | Byte | BigEndianUnicode | UTF8 | UTF7 | UTF32 | Ascii | Default | Oem | BigEndianUTF32}] [-Stream <string>] [<CommonParameters>]
示例2
Set-Content [-Value] <Object[]> -LiteralPath <string[]> [-PassThru] [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-Force] [-Credential <pscredential>] [-WhatIf] [-Confirm] [-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 - 此参数用于为内容指定备用数据流。如果流不存在,它将创建它。参数接受通配符。它是一个动态参数,由FileSystem提供程序添加到Set-Content
cmdlet。 它仅在文件系统的驱动器中起作用。
示例
示例1: 创建一个新文件并写入内容
PS E:\xntutor\powershell> set-content -path setcontent.txt -value "Windows Operating system" PS E:\xntutor\powershell> get-content .\setcontent.txt Windows Operating system
此示例中的第一个cmdlet创建一个新文件,并将字符串Windows Operating system
写入该文件。 它使用-Path
和-Value
参数在当前目录中创建一个名为pw.txt
的新文件。
在此示例中,第二个cmdlet使用Get-content
cmdlet在PowerShell控制台中显示pw.txt
文件的内容。
示例2: 替换目录中现有文件的内容。
PS E:\xntutor\powershell> set-content setcontent.txt -value "Hello, Powershell new script." PS E:\xntutor\powershell> get-content .\setcontent.txt Hello, Powershell new script.
在此示例中,替换当前目录中文件的内容。 此cmdlet使用-Value
参数提供文本字符串Hello, Powershell new script.
替换文件中的内容。
示例3: 创建一个新文件并将系统日期和时间写入文件
PS E:\xntutor\powershell> set-content date.txt -value (get-date) PS E:\xntutor\powershell> get-content date.txt 2020/2/1 20:01:28
本示例中的第一个cmdlet创建一个新文件,并将系统的当前日期和时间写入当前目录中的文件。Set-content
cmdlet使用-Path
和-Value
参数在当前目录中创建一个名为date.txt
的新文件。Get-date
获取当前日期和时间。
在此示例中,第二个cmdlet使用Get-Content
在PowerShell控制台中显示date.txt
文件的内容。
上面几个示例的执行过程如下所示:
扫描二维码
程序员编程王