- Reference
- Module:
- Microsoft.PowerShell.Management
Deletes the specified items.
Syntax
Remove-Item [-Path] <String[]> [-Filter <String>] [-Include <String[]>] [-Exclude <String[]>] [-Recurse] [-Force] [-Credential <PSCredential>] [-WhatIf] [-Confirm] [-Stream <String[]>] [<CommonParameters>]
Remove-Item -LiteralPath <String[]> [-Filter <String>] [-Include <String[]>] [-Exclude <String[]>] [-Recurse] [-Force] [-Credential <PSCredential>] [-WhatIf] [-Confirm] [-Stream <String[]>] [<CommonParameters>]
Description
The Remove-Item
cmdlet deletes one or more items. Because it is supported by many providers, itcan delete many different types of items, including files, folders, registry keys, variables,aliases, and functions.
Examples
Example 1: Delete files that have any file name extension
This example deletes all of the files that have names that include a dot (.
) from the C:\Test
folder. Because the command specifies a dot, the command does not delete folders or files that haveno file name extension.
Remove-Item C:\Test\*.*
Example 2: Delete some of the document files in a folder
This example deletes from the current folder all files that have a .doc
file name extension and aname that does not include *1*
.
Remove-Item * -Include *.doc -Exclude *1*
It uses the wildcard character (*
) to specify the contents of the current folder. It uses theInclude and Exclude parameters to specify the files to delete.
Example 3: Delete hidden, read-only files
This command deletes a file that is both hidden and read-only.
Remove-Item -Path C:\Test\hidden-RO-file.txt -Force
It uses the Path parameter to specify the file. It uses the Forceparameter to delete it. Without Force, you cannot delete read-only orhidden files.
Example 4: Delete files in subfolders recursively
This command deletes all of the CSV files in the current folder and all subfolders recursively.
Because the Recurse parameter in Remove-Item
has a known issue, the command in this exampleuses Get-ChildItem
to get the desired files, and then uses the pipeline operator to pass them toRemove-Item
.
Get-ChildItem * -Include *.csv -Recurse | Remove-Item
In the Get-ChildItem
command, Path has a value of (*
), which represents the contents of thecurrent folder. It uses Include to specify the CSV file type, and it uses Recurse to makethe retrieval recursive. If you try to specify the file type in the path, such as -Path *.csv
, thecmdlet interprets the subject of the search to be a file that has no child items, and Recursefails.
Note
This behavior was fixed in Windows versions 1909 and up.
Example 5: Delete subkeys recursively
This command deletes the "OldApp" registry key and all its subkeys and values. It uses Remove-Item
to remove the key. The path is specified, but the optional parameter name (Path) is omitted.
The Recurse parameter deletes all of the contents of the "OldApp" key recursively. If the keycontains subkeys and you omit the Recurse parameter, you are prompted to confirm that you wantto delete the contents of the key.
Remove-Item HKLM:\Software\MyCompany\OldApp -Recurse
Example 6: Deleting files with special characters
The following example shows how to delete files that contain special characters like brackets orparentheses.
Get-ChildItemDirectory: C:\temp\DownloadsMode 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].txtGet-ChildItem | Where-Object Name -Like '*`[*'Directory: C:\temp\DownloadsMode 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].txtGet-ChildItem | Where-Object Name -Like '*`[*' | ForEach-Object { Remove-Item -LiteralPath $_.Name }Get-ChildItemDirectory: C:\temp\DownloadsMode LastWriteTime Length Name---- ------------- ------ -----a--- 6/1/2018 12:19 PM 1362 myFile.txt
Example 7: Remove an alternate data stream
This example shows how to use the Stream dynamic parameter of the Remove-Item
cmdlet to deletean alternate data stream. The stream parameter is introduced in Windows PowerShell 3.0.
Get-Item C:\Test\Copy-Script.ps1 -Stream Zone.IdentifierFileName: \\C:\Test\Copy-Script.ps1Stream Length------ ------Zone.Identifier 26Remove-Item C:\Test\Copy-Script.ps1 -Stream Zone.IdentifierGet-Item C:\Test\Copy-Script.ps1 -Stream Zone.IdentifierGet-Item : Could not open alternate data stream 'Zone.Identifier' of file 'C:\Test\Copy-Script.ps1'.
The Stream parameter Get-Item
gets the Zone.Identifier
stream of the Copy-Script.ps1
file. Remove-Item
uses the Stream parameter to remove the Zone.Identifier
stream of thefile. Finally, the Get-Item
cmdlet shows that the Zone.Identifier
stream was deleted.
Parameters
-Confirm
Prompts you for confirmation before running the cmdlet. For more information, see the followingarticles:
- about_Preference_Variables
- about_Functions_CmdletBindingAttribute
Type: | SwitchParameter |
Aliases: | cf |
Position: | Named |
Default value: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Credential
Note
This parameter is not supported by any providers installed with PowerShell.To impersonate another user, or elevate your credentials when running this cmdlet,use Invoke-Command.
Type: | PSCredential |
Position: | Named |
Default value: | Current user |
Accept pipeline input: | True |
Accept wildcard characters: | False |
-Exclude
Specifies, as a string array, an item or items that this cmdlet excludes in the operation. The valueof this parameter qualifies the Path parameter. Enter a path element or pattern, such as*.txt
. Wildcard characters are permitted. The Exclude parameter is effective only when thecommand includes the contents of an item, such as C:\Windows\*
, where the wildcard characterspecifies the contents of the C:\Windows
directory.
When using Recurse with Exclude, Exclude only filters results of the current directory.If there are files that match the Exclude pattern in subfolders, those files are removed alongwith its parent directory.
Type: | String[] |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | True |
-Filter
Specifies a filter to qualify the Path parameter. TheFileSystem provider is the onlyinstalled PowerShell provider that supports the use of filters. You can find the syntax for theFileSystem filter language inabout_Wildcards. Filters are more efficientthan other parameters, because the provider applies them when the cmdlet gets the objects ratherthan having PowerShell filter the objects after they are retrieved.
Type: | String |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | True |
-Force
Forces the cmdlet to remove items that cannot otherwise be changed, such as hidden or read-onlyfiles or read-only aliases or variables. The cmdlet cannot remove constant aliases or variables.Implementation varies from provider to provider. For more information, seeabout_Providers. Even using the Forceparameter, the cmdlet cannot override security restrictions.
Type: | SwitchParameter |
Position: | Named |
Default value: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Include
Specifies, as a string array, an item or items that this cmdlet includes in the operation. The valueof this parameter qualifies the Path parameter. Enter a path element or pattern, such as"*.txt"
. Wildcard characters are permitted. The Include parameter is effective only when thecommand includes the contents of an item, such as C:\Windows\*
, where the wildcard characterspecifies the contents of the C:\Windows
directory.
Type: | String[] |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | True |
-LiteralPath
Specifies a path to one or more locations. The value of LiteralPath is used exactly as it istyped. No characters are interpreted as wildcards. If the path includes escape characters, encloseit in single quotation marks. Single quotation marks tell PowerShell not to interpret any charactersas escape sequences.
For more information, seeabout_Quoting_Rules.
Type: | String[] |
Aliases: | PSPath, LP |
Position: | Named |
Default value: | None |
Accept pipeline input: | True |
Accept wildcard characters: | False |
-Path
Specifies a path of the items being removed.Wildcard characters are permitted.
Type: | String[] |
Position: | 0 |
Default value: | None |
Accept pipeline input: | True |
Accept wildcard characters: | True |
-Recurse
Indicates that this cmdlet deletes the items in the specified locations and in all child items ofthe locations.
The Recurse parameter might not delete all subfolders or all child items. This is a known issue.
Note
This behavior was fixed in Windows versions 1909 and newer.
Type: | SwitchParameter |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Stream
Note
This Parameter is only available on Windows.
The Stream parameter is a dynamic parameter that the FileSystem provider adds to Remove-Item
.This parameter works only in file system drives.
You can use Remove-Item
to delete an alternative data stream, such as Zone.Identifier
.However, it is not the recommended way to eliminate security checks that block files that aredownloaded from the Internet. If you verify that a downloaded file is safe, use the Unblock-File
cmdlet.
This parameter was introduced in Windows PowerShell 3.0. As of Windows PowerShell 7.2, Remove-Item
can remove alternative data streams from directories as well as files.
Type: | String[] |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | True |
-WhatIf
Shows what would happen if the cmdlet runs. The cmdlet is not run.
Type: | SwitchParameter |
Aliases: | wi |
Position: | Named |
Default value: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Inputs
String
You can pipe a string that contains a path, but not a literal path, to this cmdlet.
Outputs
None
This cmdlet returns no output.
Notes
PowerShell includes the following aliases for Remove-Item
:
- All platforms:
del
erase
rd
ri
- Windows:
rm
rmdir
The Remove-Item
cmdlet is designed to work with the data exposed by any provider. To list theproviders available in your session, type Get-PsProvider
. For more information, seeabout_Providers.
When you try to delete a folder that contains items without using the Recurse parameter, thecmdlet prompts for confirmation. Using -Confirm:$false
does not suppress the prompt. This is bydesign.
- Clear-Item
- Copy-Item
- Get-Item
- Invoke-Item
- Move-Item
- New-Item
- Remove-ItemProperty
- Rename-Item
- Set-Item
- about_Providers
- about_Preference_Variables
- about_Functions_CmdletBindingAttribute
FAQs
How do I delete an item in PowerShell? ›
The Remove-Item cmdlet deletes one or more items. Because it is supported by many providers, it can delete many different types of items, including files, folders, registry keys, variables, aliases, and functions.
What is the PowerShell command to delete Remove a file? ›Cmdlet. Remove-Item cmdlet is used to delete a file by passing the path of the file to be deleted.
Which command delete the contents of an item in PowerShell? ›The Clear-Content cmdlet deletes the contents of an item, such as deleting the text from a file, but it does not delete the item. As a result, the item exists, but it is empty. Clear-Content is similar to Clear-Item , but it works on items with contents, instead of items with values.
How do I remove a specific item from a list? ›- Using the remove() method.
- Using the list object's pop() method.
- Using the del operator.
Open your phone's Files app . Tap a file. Delete .
What is remove () method? ›The remove() method removes the first occurrence of the element with the specified value.
Which commands are used to remove files? ›Use the rm command to remove files you no longer need. The rm command removes the entries for a specified file, group of files, or certain select files from a list within a directory.
What are the 2 ways to delete a file? ›Right-click the file, then click Delete on the shortcut menu. Tip: You can also select more than one file to be deleted at the same time. Press and hold the CTRL key as you select multiple files to delete.
How do I purge deleted items? ›- Select the Deleted Items folder from the Folder list.
- Click Recover Deleted Items on the Folder Tab.
- Select the item you wish to purge. ...
- Click the Purge Selected Items button, and then close the Recover Deleted Items window. ...
- The items are now permanently removed from the server and will not be recoverable.
Empty the Deleted Items folder
In the Folder pane, right-click the Deleted Items folder, and then click Empty Folder. You'll be prompted to confirm that you want to permanently delete the items.
How do I Delete something I can't Delete? ›
- Close apps. Often, the problem of a file that can't be deleted can be caused by an app that is currently using the file. ...
- Close Windows Explorer (File Explorer) That's the easy one out of the way. ...
- Reboot Windows. ...
- Use Safe Mode. ...
- Use a software deletion app. ...
- Bonus tip.
- To delete the file named myfile, type the following: rm myfile.
- To delete all the files in the mydir directory, one by one, type the following: rm -i mydir/* After each file name displays, type y and press Enter to delete the file. Or to keep the file, just press Enter.
Remove Characters From a String Using the replace() Method. The String replace() method replaces a character with a new character. You can remove a character from a string by providing the character(s) to replace as the first argument and an empty string as the second argument.
How do I delete an item in set? ›To remove an item in a set, use the remove() , or the discard() method.
How do I Remove an item from a column? ›Tip: You can delete the contents of a table row or column without deleting the table structure. To do this, select the row or column and then press the Delete key. Right-click in a table cell, row, or column you want to delete. On the menu, click Delete Cells.