Remove-Item
刪除指定的專案。
Remove-Item
[-Path] <String[]>
[-Filter <String>]
[-Include <String[]>]
[-Exclude <String[]>]
[-Recurse]
[-Force]
[-Credential <PSCredential>]
[-WhatIf]
[-Confirm]
[-UseTransaction]
[-Stream <String[]>]
[<CommonParameters>]
Remove-Item
-LiteralPath <String[]>
[-Filter <String>]
[-Include <String[]>]
[-Exclude <String[]>]
[-Recurse]
[-Force]
[-Credential <PSCredential>]
[-WhatIf]
[-Confirm]
[-UseTransaction]
[-Stream <String[]>]
[<CommonParameters>]
Remove-Item
[-Path] <String[]>
[-Filter <String>]
[-Include <String[]>]
[-Exclude <String[]>]
[-Recurse]
[-Force]
[-Credential <PSCredential>]
[-WhatIf]
[-Confirm]
[-UseTransaction]
[-DeleteKey] <CommonParameters>]
Remove-Item
-LiteralPath <String[]>
[-Filter <String>]
[-Include <String[]>]
[-Exclude <String[]>]
[-Recurse]
[-Force]
[-Credential <PSCredential>]
[-WhatIf]
[-Confirm]
[-UseTransaction]
[-DeleteKey]
[<CommonParameters>]
Remove-Item
[-Path] <string[]>
[-Filter <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Recurse]
[-Force]
[-Credential <pscredential>]
[-WhatIf]
[-Confirm]
[-UseTransaction]
[<CommonParameters>]
Remove-Item
-LiteralPath <string[]>
[-Filter <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Recurse]
[-Force]
[-Credential <pscredential>]
[-WhatIf]
[-Confirm]
[-UseTransaction]
[<CommonParameters>]
Remove-Item
Cmdlet 會刪除一或多個專案。 因為許多提供者都支援它,所以可以刪除許多不同類型的專案,包括檔案、資料夾、登錄機碼、變數、別名和函式。
本範例會從 C:\Test
資料夾中刪除名稱包含點 (.
) 的所有檔案。
因為命令會指定點,因此命令不會刪除沒有擴展名的資料夾或檔案。
Remove-Item C:\Test\*.*
此範例會從目前資料夾刪除具有 .doc
擴展名的所有檔案,以及不包含 *1*
的名稱。
Remove-Item * -Include *.doc -Exclude *1*
它會使用通配符 (*
) 來指定目前資料夾的內容。 它會使用 Include 和 Exclude 參數來指定要刪除的檔案。
此命令會刪除 隱藏 和 只讀的檔案。
Remove-Item -Path C:\Test\hidden-RO-file.txt -Force
它會使用 Path 參數來指定檔案。 它會使用 Force 參數來刪除它。 如果沒有 Force,您無法刪除 唯讀 或 隱藏 檔案。
此命令會以遞歸方式刪除目前資料夾和所有子資料夾中的所有 CSV 檔案。
由於 Remove-Item
中的 Recurse 參數有已知問題,因此此範例中的命令會使用 Get-ChildItem
取得所需的檔案,然後使用管線運算符將它們傳遞至 Remove-Item
。
Get-ChildItem * -Include *.csv -Recurse | Remove-Item
在 Get-ChildItem
命令中,Path 的值為 (*
),代表目前資料夾的內容。 它會使用 Include 來指定 CSV 檔案類型,並使用 Recurse 來使擷取遞歸。 如果您嘗試在路徑中指定檔類型,例如 -Path *.csv
,Cmdlet 會將搜尋的主旨解譯為沒有子項目的檔案,Recurse 失敗。
注意
此行為已在 Windows 1909 版和更新版本中修正。
此命令會刪除 「OldApp」 登錄機碼及其所有子機碼和值。 它會使用 Remove-Item
來移除金鑰。 指定路徑,但省略選擇性參數名稱 (Path)。
Recurse 參數會以遞歸方式刪除 “OldApp” 金鑰的所有內容。 如果機碼包含子機碼,且您省略 Recurse 參數,系統會提示您確認您想要刪除金鑰的內容。
Remove-Item HKLM:\Software\MyCompany\OldApp -Recurse
下列範例示範如何刪除包含括弧或括弧等特殊字元的檔案。
Get-ChildItem
Directory: C:\temp\Downloads
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 6/1/2018 12:19 PM 1362 myFile.txt
-a--- 6/1/2018 12:30 PM 1132 myFile[1].txt
-a--- 6/1/2018 12:19 PM 1283 myFile[2].txt
-a--- 6/1/2018 12:19 PM 1432 myFile[3].txt
Get-ChildItem | Where-Object Name -Like '*`[*'
Directory: C:\temp\Downloads
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 6/1/2018 12:30 PM 1132 myFile[1].txt
-a--- 6/1/2018 12:19 PM 1283 myFile[2].txt
-a--- 6/1/2018 12:19 PM 1432 myFile[3].txt
Get-ChildItem | Where-Object Name -Like '*`[*' | ForEach-Object { Remove-Item -LiteralPath $_.Name }
Get-ChildItem
Directory: C:\temp\Downloads
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 6/1/2018 12:19 PM 1362 myFile.txt
此範例示範如何使用 Remove-Item
Cmdlet 的 Stream 動態參數來刪除替代數據流。 數據流參數是在 Windows PowerShell 3.0 中引進的。
Get-Item C:\Test\Copy-Script.ps1 -Stream Zone.Identifier
FileName: \\C:\Test\Copy-Script.ps1
Stream Length
------ ------
Zone.Identifier 26
Remove-Item C:\Test\Copy-Script.ps1 -Stream Zone.Identifier
Get-Item C:\Test\Copy-Script.ps1 -Stream Zone.Identifier
Get-Item : Could not open alternate data stream 'Zone.Identifier' of file 'C:\Test\Copy-Script.ps1'.
At line:1 char:1
+ Get-Item 'C:\Test\Copy-Script.ps1' -Stream Zone.Identifier
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Test\Copy-Script.ps1:String) [Get-Item], FileNotFoundException
+ FullyQualifiedErrorId : AlternateDataStreamNotFound,Microsoft.PowerShell.Commands.GetItemCommand
Stream 參數 Get-Item
取得 Copy-Script.ps1
檔案的 Zone.Identifier
數據流。
Remove-Item
會使用 Stream 參數來移除檔案的 Zone.Identifier
數據流。 最後,Get-Item
Cmdlet 會顯示已刪除 Zone.Identifier
數據流。
在執行 Cmdlet 之前,提示您進行確認。 如需詳細資訊,請參閱下列文章:
類型: | SwitchParameter |
別名: | cf |
Position: | Named |
預設值: | False |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
注意
任何與 PowerShell 一起安裝的提供者都不支援此參數。 若要模擬其他使用者,或在執行此 Cmdlet 時提升您的認證,請使用 Invoke-Command。
類型: | PSCredential |
Position: | Named |
預設值: | Current user |
必要: | False |
接受管線輸入: | True |
接受萬用字元: | False |
這是由 憑證 提供者提供的動態參數。 憑證 提供者和此參數只能在 Windows 平臺上使用。
提供時,Cmdlet 會在刪除憑證時刪除私鑰。
如需詳細資訊,請參閱 about_Certificate_Provider。
類型: | SwitchParameter |
Position: | Named |
預設值: | False |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
指定此 Cmdlet 在作業中排除的專案或專案,做為字串陣列。 此參數的值會限定 path 參數。 輸入路徑專案或模式,例如 *.txt
。 允許通配符。 只有在命令包含項目的內容,例如 C:\Windows\*
時,Exclude 參數才有效,其中通配符會指定 C:\Windows
目錄的內容。
搭配 Exclude使用 Recurse 時,Exclude 只會篩選目前目錄的結果。 如果子資料夾中有符合 排除 模式的檔案,這些檔案會連同其父目錄一起移除。
類型: | String[] |
Position: | Named |
預設值: | None |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | True |
指定篩選條件,以限定 Path 參數。 FileSystem 提供者是唯一支援使用篩選的 PowerShell 提供者。 您可以在 about_Wildcards中找到 FileSystem 篩選語言的語法。 篩選比其他參數更有效率,因為提供者會在 Cmdlet 取得物件時套用它們,而不是在擷取對象之後讓 PowerShell 篩選物件。
類型: | String |
Position: | Named |
預設值: | None |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | True |
強制 Cmdlet 移除無法變更的專案,例如隱藏或唯讀檔案或只讀別名或變數。 Cmdlet 無法移除常數別名或變數。 實作會因提供者而異。 如需詳細資訊,請參閱 about_Providers。 即使使用 Force 參數,Cmdlet 也無法覆寫安全性限制。
類型: | SwitchParameter |
Position: | Named |
預設值: | False |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
指定此 Cmdlet 包含在作業中的專案,做為字串陣列。 此參數的值會限定 path 參數。 輸入路徑專案或模式,例如 "*.txt"
。 允許通配符。 只有當命令包含項目的內容,例如 C:\Windows\*
時,Include 參數才有效,其中通配符會指定 C:\Windows
目錄的內容。
類型: | String[] |
Position: | Named |
預設值: | None |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | True |
指定一或多個位置的路徑。 LiteralPath 的值會完全依照其類型一樣使用。 不會將任何字元解譯為通配符。 如果路徑包含逸出字元,請以單引弧括住它。 單引號會告知PowerShell不要將任何字元解譯為逸出序列。
如需詳細資訊,請參閱 about_Quoting_Rules。
類型: | String[] |
別名: | PSPath |
Position: | Named |
預設值: | None |
必要: | True |
接受管線輸入: | True |
接受萬用字元: | False |
指定要移除之項目的路徑。 允許通配符。
類型: | String[] |
Position: | 0 |
預設值: | None |
必要: | True |
接受管線輸入: | True |
接受萬用字元: | True |
表示此 Cmdlet 會刪除指定位置及所有位置子專案中的專案。
Recurse 參數可能不會刪除所有子資料夾或所有子專案。 這是已知問題。
注意
此行為已在 Windows 1909 版和更新版本中修正。
類型: | SwitchParameter |
Position: | Named |
預設值: | None |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
這是 FileSystem 提供者所提供的動態參數。 此參數僅適用於 Windows。 此參數無法與 Recurse 參數搭配使用。
您可以使用 Remove-Item
來移除替代資料流,例如 Zone.Identifier
。
不過,不建議排除安全性檢查,以封鎖從因特網下載的檔案。 如果您確認下載的檔案是安全的,請使用 Unblock-File
Cmdlet。
此參數是在 Windows PowerShell 3.0 中引進的。
如需詳細資訊,請參閱 about_FileSystem_Provider。
類型: | String[] |
Position: | Named |
預設值: | None |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | True |
在作用中交易中包含 命令。 只有在交易進行中時,此參數才有效。 如需詳細資訊,請參閱 about_Transactions
類型: | SwitchParameter |
別名: | usetx |
Position: | Named |
預設值: | False |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
顯示 Cmdlet 執行時會發生什麼事。 Cmdlet 未執行。
類型: | SwitchParameter |
別名: | wi |
Position: | Named |
預設值: | False |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
您可以使用管線將包含路徑但不是常值路徑的字串傳送至此 Cmdlet。
None
此 Cmdlet 不會傳回任何輸出。
Windows PowerShell 包含下列 Remove-Item
別名:
del
erase
rd
ri
rm
rmdir
Remove-Item
Cmdlet 的設計目的是要處理任何提供者所公開的數據。 若要列出工作階段中可用的提供者,請輸入 Get-PsProvider
。 如需詳細資訊,請參閱 about_Providers。
當您嘗試刪除包含項目的資料夾,而不使用 Recurse 參數時,Cmdlet 會提示確認。 使用 -Confirm:$false
不會隱藏提示。 這是根據設計。