Set-Content

寫入新內容或取代檔案中的現有內容。

Syntax

Set-Content
   [-Path] <string[]>
   [-Value] <Object[]>
   [-PassThru]
   [-Filter <string>]
   [-Include <string[]>]
   [-Exclude <string[]>]
   [-Force]
   [-Credential <pscredential>]
   [-WhatIf]
   [-Confirm]
   [-NoNewline]
   [-Encoding <Encoding>]
   [-AsByteStream]
   [-Stream <string>]
   [<CommonParameters>]
Set-Content
   [-Value] <Object[]>
   -LiteralPath <string[]>
   [-PassThru]
   [-Filter <string>]
   [-Include <string[]>]
   [-Exclude <string[]>]
   [-Force]
   [-Credential <pscredential>]
   [-WhatIf]
   [-Confirm]
   [-NoNewline]
   [-Encoding <Encoding>]
   [-AsByteStream]
   [-Stream <string>]
   [<CommonParameters>]
Set-Content
   [-Path] <string[]>
   [-Value] <Object[]>
   [-PassThru]
   [-Filter <string>]
   [-Include <string[]>]
   [-Exclude <string[]>]
   [-Force]
   [-Credential <pscredential>]
   [-WhatIf]
   [-Confirm]
   [<CommonParameters>]
Set-Content
   [-Value] <Object[]>
   -LiteralPath <string[]>
   [-PassThru]
   [-Filter <string>]
   [-Include <string[]>]
   [-Exclude <string[]>]
   [-Force]
   [-Credential <pscredential>]
   [-WhatIf]
   [-Confirm]
   [<CommonParameters>]

Description

Set-Content 是字串處理 Cmdlet,可寫入新內容或取代檔案中的內容。 Set-Content 會取代現有的內容,而且與將內容附加至檔案的 Add-Content Cmdlet 不同。 若要將內容傳送至 Set-Content ,您可以使用 命令行上的 Value 參數,或透過管線傳送內容。

如果您需要建立下列範例的檔案或目錄,請參閱 New-Item

範例

範例 1:取代目錄中多個檔案的內容

本範例會取代目前目錄中多個檔案的內容。

Get-ChildItem -Path .\Test*.txt

Test1.txt
Test2.txt
Test3.txt

Set-Content -Path .\Test*.txt -Value 'Hello, World'
Get-Content -Path .\Test*.txt

Hello, World
Hello, World
Hello, World

Cmdlet 會使用 Path 參數來列出以目前目錄中開頭Test*的檔案.txtGet-ChildItem Cmdlet Set-Content 會使用 Path 參數來指定 Test*.txt 檔案。 Value 參數會提供文字字串 Hello, World 來取代每個檔案中的現有內容。 Cmdlet Get-Content 會使用 Path 參數來指定檔案, Test*.txt 並在 PowerShell 控制台中顯示每個檔案的內容。

範例 2:建立新的檔案並寫入內容

此範例會建立新的檔案,並將目前的日期和時間寫入檔案。

Set-Content -Path .\DateTime.txt -Value (Get-Date)
Get-Content -Path .\DateTime.txt

1/30/2019 09:55:08

Set-Content使用PathValue 參數,在目前目錄中建立名為 DateTime.txt 的新檔案。 Value 參數會使用 Get-Date 來取得目前的日期和時間。 Set-ContentDateTime 物件寫入檔案做為字串。 Cmdlet Get-Content使用Path參數,在PowerShell控制台中顯示DateTime.txt的內容

範例 3:取代檔案中的文字

此命令會取代現有檔案內 Word 的所有實例。

Get-Content -Path .\Notice.txt

Warning
Replace Warning with a new word.
The word Warning was replaced.

(Get-Content -Path .\Notice.txt) |
    ForEach-Object {$_ -Replace 'Warning', 'Caution'} |
        Set-Content -Path .\Notice.txt
Get-Content -Path .\Notice.txt

Caution
Replace Caution with a new word.
The word Caution was replaced.

Cmdlet Get-Content 會使用 Path 參數來指定 目前目錄中的Notice.txt 檔案。 命令 Get-Content 會以括號包裝,讓命令在傳送至管線之前完成。

Notice.txt檔案的內容會向下傳送至 ForEach-Object Cmdlet。 ForEach-Object 會使用自動變數 $_ ,並將每個出現的 Warning 取代為 警告。 物件會向下傳送至 Cmdlet 的 Set-Content 管線。 Set-Content會使用Path參數來指定Notice.txt檔案,並將更新的內容寫入檔案。

最後一個 Get-Content Cmdlet 會在 PowerShell 控制台中顯示更新的檔案內容。

範例 4:搭配 Set-Content 使用篩選

您可以指定 Cmdlet 的篩選條件 Set-Content 。 使用篩選來限定 Path 參數時,您必須包含尾端星號 (*) 以指出路徑的內容。

下列命令會將目錄中所有 *.txt 檔案 C:\Temp 的內容設定為 空白值

Set-Content -Path C:\Temp\* -Filter *.txt -Value "Empty"

參數

-AsByteStream

這是 FileSystem 提供者提供的動態參數。 如需詳細資訊,請參閱 about_FileSystem_Provider

指定內容應該寫入為位元組數據流。 此參數是在 PowerShell 6.0 中引進的。

當您搭配 Encoding 參數使用 AsByteStream 參數時,就會發生警告。 AsByteStream 參數會忽略任何編碼,而輸出會寫入為位元組數據流。

Type:SwitchParameter
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Confirm

執行 Cmdlet 之前先提示您確認。

Type:SwitchParameter
Aliases:cf
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Credential

注意

任何與 PowerShell 一起安裝的提供者都不支援此參數。 若要模擬其他使用者,或在執行此 Cmdlet 時提升您的認證,請使用 Invoke-Command

Type:PSCredential
Position:Named
Default value:None
Required:False
Accept pipeline input:True
Accept wildcard characters:False

-Encoding

這是 FileSystem 提供者提供的動態參數。 如需詳細資訊,請參閱 about_FileSystem_Provider

指定目標檔案的編碼類型。 預設值是 utf8NoBOM

編碼是 FileSystem 提供者新增至 Set-Content的動態參數。 此參數僅適用於檔案系統磁碟驅動器。

此參數可接受的值如下:

  • ascii:使用 ASCII (7 位) 字元集的編碼方式。
  • ansi:針對目前文化特性的 ANSI 代碼頁,使用 的編碼方式。 此選項已在PowerShell 7.4中新增。
  • bigendianunicode:使用 big-endian 位元組順序以 UTF-16 格式編碼。
  • bigendianutf32:使用 big-endian 位元組順序以 UTF-32 格式編碼。
  • oem:使用 MS-DOS 和控制台程式的預設編碼方式。
  • unicode:使用位元組順序以UTF-16格式編碼。
  • utf7:以 UTF-7 格式編碼。
  • utf8:以 UTF-8 格式編碼。
  • utf8BOM:使用位元節順序標記以 UTF-8 格式編碼 (BOM)
  • utf8NoBOM:以 UTF-8 格式編碼,不含位元組順序標記 (BOM)
  • utf32:以 UTF-32 格式編碼。

從 PowerShell 6.2 開始,Encoding 參數也允許已註冊代碼頁的數值識別元(例如 -Encoding 1251)或已註冊代碼頁的字串名稱(例如 )。-Encoding "windows-1251" 如需詳細資訊,請參閱 Encoding.CodePage.NET 檔。

從 PowerShell 7.4 開始,您可以使用 Ansi Encoding 參數的值,傳遞目前文化特性 ANSI 代碼頁的數值標識碼,而不需要手動指定它。

注意

不再建議使用UTF-7* 。 自 PowerShell 7.1 起,如果您為 Encoding 參數指定utf7,則會撰寫警告。

Type:Encoding
Accepted values:ASCII, BigEndianUnicode, BigEndianUTF32, OEM, Unicode, UTF7, UTF8, UTF8BOM, UTF8NoBOM, UTF32
Position:Named
Default value:utf8NoBOM
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Exclude

指定此 Cmdlet 在作業中排除的專案或專案,做為字串陣列。 此參數的值會 限定Path 參數。 輸入路徑專案或模式,例如 *.txt。 允許通配符。 Exclude 參數只有在命令包含項目的內容時有效,例如 C:\Windows\*,通配符會指定目錄的內容C:\Windows

Type:String[]
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:True

-Filter

指定篩選條件以限定 Path 參數。 FileSystem 提供者是唯一支援使用篩選器的已安裝 PowerShell 提供者。 您可以在 about_Wildcards 中找到 FileSystem 篩選語言的語法。 篩選比其他參數更有效率,因為提供者會在 Cmdlet 取得物件時套用它們,而不是在擷取對象之後讓 PowerShell 篩選物件。

Type:String
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:True

-Force

強制 Cmdlet 設定檔案的內容,即使檔案是唯讀的。 實作會因提供者而異。 如需詳細資訊,請參閱 about_ProvidersForce 參數不會覆寫安全性限制。

Type:SwitchParameter
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Include

指定此 Cmdlet 包含在作業中的專案,做為字串陣列。 此參數的值會 限定Path 參數。 輸入路徑專案或模式,例如 "*.txt"。 允許通配符。 Include 參數只有在命令包含項目的內容時有效,例如 C:\Windows\*,其中通配符會指定目錄的內容C:\Windows

Type:String[]
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:True

-LiteralPath

指定一或多個位置的路徑。 LiteralPath 的值會與類型完全相同使用。 不會將任何字元解譯為通配符。 如果路徑包含逸出字元,請以單引弧括住它。 單引號會告知PowerShell不要將任何字元解譯為逸出序列。

如需詳細資訊,請參閱 about_Quoting_Rules

Type:String[]
Aliases:PSPath, LP
Position:Named
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-NoNewline

這是 FileSystem 提供者提供的動態參數。 如需詳細資訊,請參閱 about_FileSystem_Provider

輸入物件的字串表示會串連以形成輸出。 輸出字串之間不會插入空格或換行符。 最後一個輸出字串之後不會新增任何換行符。

Type:SwitchParameter
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-PassThru

傳回物件,表示內容。 根據預設,此 Cmdlet 不會產生任何輸出。

Type:SwitchParameter
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Path

指定接收內容之項目的路徑。 允許通配符。

Type:String[]
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:True

-Stream

這是 FileSystem 提供者提供的動態參數。 此參數僅適用於 Windows。 如需詳細資訊,請參閱 about_FileSystem_Provider

指定內容的替代數據流。 如果數據流不存在,此 Cmdlet 會建立它。 不支援萬用字元。

Stream 是 FileSystem 提供者新增至 Set-Content動態參數。 此參數僅適用於檔案系統磁碟驅動器。

您可以使用 Set-Content Cmdlet 來建立或更新任何替代資料串流的內容, 例如 Zone.Identifier。 不過,我們不建議這樣做,以消除封鎖從因特網下載之檔案的安全性檢查。 如果您確認下載的檔案是安全的,請使用 Unblock-File Cmdlet。

此參數是在 PowerShell 3.0 中引進的。 從 PowerShell 7.2 起, Set-Content 可以從目錄和檔案設定替代數據流的內容。

Type:String
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Value

指定專案的新內容。

Type:Object[]
Position:1
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-WhatIf

顯示執行 Cmdlet 後會發生的情況。 未執行 Cmdlet。

Type:SwitchParameter
Aliases:wi
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

輸入

Object

您可以使用管線將包含專案新值的物件傳送至這個 Cmdlet。

輸出

None

根據預設,此 Cmdlet 不會傳回任何輸出。

String

當您使用 PassThru 參數時,這個 Cmdlet 會傳回代表內容的字串。

備註

  • Set-Content 是針對字串處理所設計。 如果您使用管線將非字串物件傳送至 Set-Content,則會在寫入物件之前,將物件轉換成字串。 若要將物件寫入檔案,請使用 Out-File
  • Cmdlet Set-Content 的設計目的是要處理任何提供者所公開的數據。 若要列出工作階段中可用的提供者,請輸入 Get-PsProvider。 如需詳細資訊,請參閱 about_Providers