Set-Content
寫入新內容或取代檔案中的現有內容。
語法
Path (預設值)
Set-Content
[-Path] <string[]>
[-Value] <Object[]>
[-PassThru]
[-Filter <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Force]
[-Credential <pscredential>]
[-WhatIf]
[-Confirm]
[-UseTransaction]
[-NoNewline]
[-Encoding <FileSystemCmdletProviderEncoding>]
[-Stream <string>]
[<CommonParameters>]
LiteralPath
Set-Content
[-Value] <Object[]>
-LiteralPath <string[]>
[-PassThru]
[-Filter <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Force]
[-Credential <pscredential>]
[-WhatIf]
[-Confirm]
[-UseTransaction]
[-NoNewline]
[-Encoding <FileSystemCmdletProviderEncoding>]
[-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
Get-ChildItem Cmdlet 會使用 Path 參數 .txt 來列出以目前目錄中 Test* 開頭的檔案。
Set-Content Cmdlet 會使用 Path 參數來指定 Test*.txt 檔案。
Value 參數會提供取代每個檔案中現有內容的文字字串 Hello、World。
Get-Content Cmdlet 會使用 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 會使用 Path 和 Value 參數,在目前目錄中建立名為 DateTime.txt 的新檔案。
Value 參數會使用 Get-Date 來取得目前的日期和時間。
Set-Content 會將 DateTime 物件寫入檔案作為字串。
Get-Content Cmdlet 會使用 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.
Get-Content Cmdlet 會使用 Path 參數來指定目前目錄中的 Notice.txt 檔案。
Get-Content 命令會以括號包裝,讓命令在傳送至管線之前完成。
Notice.txt 檔案的內容會向下傳送至 ForEach-Object Cmdlet。
ForEach-Object 使用自動變數 $_,並以 Warning取代每個出現的 Warning。 物件會向下傳送至 Set-Content Cmdlet。
Set-Content 會使用 Path 參數來指定 Notice.txt 檔案,並將更新的內容寫入檔案。
最後一個 Get-Content Cmdlet 會在 PowerShell 控制台中顯示更新的檔案內容。
範例 4:搭配使用篩選搭配 Set-Content
您可以指定 Set-Content Cmdlet 的篩選條件。 使用篩選條件來限定 Path 參數時,您必須包含尾端星號 (*) 以指出路徑的內容。
下列命令會將 *.txt 目錄中所有 C:\Temp 檔案的內容設定為空白 值。
Set-Content -Path C:\Temp\* -Filter *.txt -Value "Empty"
參數
-Confirm
在執行 Cmdlet 之前,提示您進行確認。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | cf |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Credential
備註
任何與 PowerShell 一起安裝的提供者都不支援此參數。 若要模擬其他使用者,或在執行此 Cmdlet 時提升您的認證,請使用 Invoke-Command。
參數屬性
| 類型: | PSCredential |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Encoding
指定目標檔案的編碼類型。 預設值為 Default。
編碼是 FileSystem 提供者新增至 Set-Content的動態參數。 此參數僅適用於檔案系統磁碟驅動器。
此參數可接受的值如下:
- ASCII 使用 ASCII (7 位) 字元集。
- BigEndianUnicode 使用 UTF-16 與 big-endian 位元組順序。
- BigEndianUTF32 使用 UTF-32 搭配 big-endian 位元組順序。
- 位元組 將一組字元編碼成位元組序列。
- 預設 使用對應至系統使用中代碼頁的編碼方式(通常是 ANSI)。
- OEM 使用對應至系統目前 OEM 代碼頁的編碼方式。
- 字串 與 unicode 相同。
- Unicode 使用 UTF-16 搭配位元組順序。
未知 與 Unicode相同。 - UTF7 使用 UTF-7。
- UTF8 使用 UTF-8。
- UTF32 使用 UTF-32 搭配位元組順序。
編碼是 FileSystem 提供者新增至 Set-Content的動態參數。 此參數僅適用於檔案系統磁碟驅動器。
參數屬性
| 類型: | FileSystemCmdletProviderEncoding |
| 預設值: | Default |
| 接受的值: | ASCII, BigEndianUnicode, BigEndianUTF32, Byte, Default, OEM, String, Unicode, Unknown, UTF7, UTF8, UTF32 |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Exclude
指定要在此 Cmdlet 作業中排除的項目或多項,並以字串陣列形式表示。 此參數的值對 路徑 參數進行限定。 輸入路徑元素或模式,例如 *.txt。 允許使用通配符字元。 只有在命令包含項目的內容,例如 時,C:\Windows\* 參數才有效,其中通配符會指定 C:\Windows 目錄的內容。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | True |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Filter
指定篩選條件,以限定 Path 參數。 FileSystem 提供者是唯一已安裝且支援使用篩選的 PowerShell 提供者。 您可以在 about_Wildcards中找到 FileSystem 篩選語言的語法。 篩選比其他參數更有效率,因為提供者會在 Cmdlet 取得物件時套用它們,而不是在擷取對象之後讓 PowerShell 篩選物件。
參數屬性
| 類型: | String |
| 預設值: | None |
| 支援萬用字元: | True |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Force
強制 Cmdlet 設定檔案的內容,即使檔案是唯讀的。 實作會因提供者而異。 如需詳細資訊,請參閱 about_Providers。 Force 參數不會覆寫安全性限制。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Include
指定此 Cmdlet 在操作中包含的一個或多個專案,這些專案是以字串陣列的形式表示。 此參數的值對 路徑 參數進行限定。 輸入路徑元素或模式,例如 "*.txt"。 允許使用通配符字元。 只有當命令包含項目的內容,例如 時,C:\Windows\* 參數才有效,其中通配符會指定 C:\Windows 目錄的內容。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | True |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-LiteralPath
指定通往一個或多個位置的路徑。 LiteralPath 的值會按原本輸入的方式使用。 不會將任何字元解譯為通配符。 如果路徑包含逸出字元,請以單引弧括住它。 單引號會告知PowerShell不要將任何字元解譯為逸出序列。
如需詳細資訊,請參閱 about_Quoting_Rules。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | PSPath |
參數集
LiteralPath
| Position: | Named |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-NoNewline
輸入物件的字串表示會串連以形成輸出。 輸出字串之間不會插入空格或換行符。 最後一個輸出字串之後不會新增任何換行符。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-PassThru
傳回物件,表示內容。 根據預設,此 Cmdlet 不會產生任何輸出。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Path
指定接收內容之項目的路徑。 允許使用通配符字元。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | True |
| 不要顯示: | False |
參數集
Path
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Stream
指定內容的替代數據流。 如果數據流不存在,此 Cmdlet 會建立它。 不支援萬用字元。
Stream 是 FileSystem 提供者新增至 Set-Content的動態參數。 此參數僅適用於檔案系統磁碟驅動器。
您可以使用 Set-Content Cmdlet 來變更 Zone.Identifier 替代數據流的內容。 不過,我們不建議這樣做,以消除封鎖從因特網下載之檔案的安全性檢查。 如果您確認下載的檔案是安全的,請使用 Unblock-File Cmdlet。
此參數是在 PowerShell 3.0 中引進的。
參數屬性
| 類型: | String |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-UseTransaction
在作用中交易中包含 命令。 只有在交易進行中時,此參數才有效。 如需詳細資訊,請參閱 about_Transactions。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | usetx |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Value
指定專案的新內容。
參數屬性
| 類型: | Object[] |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | 1 |
| 必要: | True |
| 來自管線的值: | True |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-WhatIf
顯示 Cmdlet 執行時會發生什麼事。 指令未執行。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | 無線 |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
CommonParameters
此 Cmdlet 支援一般參數:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 如需詳細資訊,請參閱 about_CommonParameters。
輸入
Object
您可以使用管線將包含專案新值的物件傳送至 Set-Content。
輸出
None or System.String
當您使用 PassThru 參數時,Set-Content 會產生代表內容的 System.String 物件。 否則,此 Cmdlet 不會產生任何輸出。
備註
- 您也可以透過內建別名
Set-Content來參考sc。 如需詳細資訊,請參閱 about_Aliases。 -
Set-Content是針對字串處理所設計。 如果您使用管線將非字串物件傳送至Set-Content,則會在寫入物件之前,將對象轉換成字串。 若要將物件寫入檔案,請使用Out-File。 -
Set-ContentCmdlet 的設計目的是要處理任何提供者所公開的數據。 若要列出工作階段中可用的提供者,請輸入Get-PsProvider。 如需詳細資訊,請參閱 about_Providers。