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>]
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 Get-ChildItem
會使用 Path 參數來列出以目前目錄中開頭Test*
的.txt 檔案。 Cmdlet 會 Set-Content
使用 Path 參數來指定 Test*.txt
檔案。 Value 參數會提供文字字串 Hello World,以取代每個檔案中的現有內容。 Cmdlet Get-Content
會使用 Path 參數來指定檔案, Test*.txt
並在 PowerShell 控制台中顯示每個檔案的內容。
範例 2:Create 新的檔案和寫入內容
此範例會建立新的檔案,並將目前的日期和時間寫入檔案。
Set-Content -Path .\DateTime.txt -Value (Get-Date)
Get-Content -Path .\DateTime.txt
1/30/2019 09:55:08
Set-Content
會使用 Path 和 Value 參數,在目前目錄中建立名為 DateTime.txt 的新檔案。 Value 參數會使用 Get-Date
來取得目前的日期和時間。
Set-Content
將 DateTime 物件寫入檔案做為字串。 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 檔案的內容會向下傳送至 Cmdlet。ForEach-Object
ForEach-Object
會使用自動變數$_
,並以警告取代每個出現的警告。 物件會向下傳送至 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
指定內容應讀取為位元組數據流。 此參數是在 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
指定目標檔案的編碼類型。 預設值是 utf8NoBOM
。
編碼是 FileSystem 提供者新增至 Set-Content
的動態參數。 此參數只適用於檔案系統磁碟機。
此參數可接受的值如下所示:
ascii
:使用 ASCII (7 位) 字元集的編碼方式。bigendianunicode
:使用大位元組位元組順序以UTF-16格式編碼。oem
:使用 MS-DOS 和控制台程式的預設編碼方式。unicode
:使用位元組由小到大的順序,以UTF-16格式編碼。utf7
:以 UTF-7 格式編碼。utf8
:以 UTF-8 格式編碼。utf8BOM
:使用位元組順序標記 (BOM) 以 UTF-8 格式編碼utf8NoBOM
:以 UTF-8 格式編碼,不含位元組順序標記 (BOM)utf32
:以 UTF-32 格式編碼。
從 PowerShell 6.2 開始, Encoding 參數也允許已註冊代碼頁的數值標識符, (例如 -Encoding 1251
) 或已註冊代碼頁的字串名稱 (,例如 -Encoding "windows-1251"
) 。 如需詳細資訊,請參閱 Encoding.CodePage 的 .NET 檔。
Type: | Encoding |
Accepted values: | ASCII, BigEndianUnicode, 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_Providers。 Force 參數不會覆寫安全性限制。
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
輸入物件的字串表示會串連以形成輸出。 輸出字串之間不會插入空格或換行符。 最後一個輸出字串之後不會新增任何換行符。
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
指定內容的替代數據流。 如果數據流不存在,此 Cmdlet 會建立它。 不支援萬用字元。
Stream 是 FileSystem 提供者新增至 Set-Content
的動態參數。 此參數只適用於檔案系統磁碟機。
您可以使用 Set-Content
Cmdlet 來變更 Zone.Identifier 替代數據流的內容。 不過,我們不建議這麼做,以消除封鎖從因特網下載之檔案的安全性檢查。 如果您確認下載的檔案安全,請使用 Unblock-File
Cmdlet。
此參數是在 PowerShell 3.0 中引進。
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 |
輸入
您可以使用管線將包含專案新值的物件傳送至 Set-Content
。
輸出
None or System.String
當您使用 PassThru 參數時, Set-Content
會產生代表內容的 System.String 物件。 否則,此 Cmdlet 不會產生任何輸出。
備註
- 您也可以
Set-Content
參考其內建別名 。sc
如需詳細資訊,請參閱 about_Aliases。 Set-Content
專為字串處理而設計。 如果您使用管線將非字串物件傳送至Set-Content
,它會先將對象轉換成字串,然後再寫入字串。 若要將物件寫入檔案,請使用Out-File
。- 此
Set-Content
Cmdlet 的設計目的是要處理任何提供者所公開的數據。 若要列出工作階段中可用的提供者,請輸入Get-PsProvider
。 如需詳細資訊,請參閱 about_Providers。