Delete the specified items.

      Remove-Item { [-path] string[] | [-literalPath] string[] }
         [-include string[]] [-exclude string[]] [-filter string] [-Stream String[]]
            [-recurse] [-force] [-whatIf] [-confirm]
               [-credential PSCredential] [-UseTransaction] [CommonParameters]
      Remove-Item [-Stream string] [CommonParameters]

   -Path string
       The path(s) to the items. Wildcards are permitted.
       Use the wildcard (*) to specify all items in the current location.

   -LiteralPath string
       Like Path above, only the value is used exactly as typed.
       No characters are interpreted as wildcards. If the path includes any
       escape characters then enclose the path in single quotation marks.

   -Include string
       Include only the specified items from the Path. e.g. "May*"
       this only works when the path includes a wildcard character.

   -Exclude string
       Omit the specified items from the Path e.g. "*SS64*"
       this only works when the path includes a wildcard character.

   -Filter string
       A filter in the provider's format or language. 
       The exact syntax of the filter (wildcard support etc) depends on the provider.
       Filters are more efficient than -include/-exclude, because the provider
       applies the filter when retrieving the objects, rather than having 
       PowerShell filter the objects after they are retrieved.

       Also delete child items from the specified location.
       When used with -Include, -Recurse may not delete all subfolders or all child items.
       This is a known issue.This behavior was fixed in Windows versions 1909 and newer.
       As a workaround, try piping results of the `Get-ChildItem -Recurse` command to Remove-Item

       Override restrictions that prevent the command from succeeding, apart from 
       security settings. e.g. -force will override a files read-only or hidden
       attribute, but will not change file permissions.
       The cmdlet cannot remove constant aliases or variables.

   -Credential PSCredential
       Use a credential to validate access to the file. Credential represents
       a user-name, such as "User01" or "Domain01\User01", or a PSCredential
       object, such as the one retrieved by using the Get-Credential cmdlet.
       If you type a user name, you will be prompted for a password.
       This parameter is not supported by any PowerShell core cmdlets or providers.

   -Stream string
       Delete the specified alternate data stream from a file, but do not delete the file.
       Enter the stream name. Wildcards are supported. This parameter is not valid on folders.

       Stream is a dynamic parameter that the FileSystem provider adds to the Remove-Item cmdlet.
       This parameter works only in file system drives.

       You can use the Remove-Item cmdlet to delete an alternate data stream. However, it is not the recommended way 
       to eliminate security checks that block files that are downloaded from the Internet. To verify that a 
       downloaded file is safe, use the Unblock-File cmdlet.  PowerShell 3.0+

       Describe what would happen if you executed the command without
       actually executing the command.

       Prompt for confirmation before executing the command.

       Include the command in the active transaction.

Standard Aliases for Remove-Item: del, erase, rd, rm, rmdir, ri

When used with the -Include parameter, the -Recurse parameter might not delete all subfolders or all child items. This is a known issue. As a workaround, pipe `Get-ChildItem -Recurse` to Remove-Item. This behavior was fixed in Windows versions 1909 and up.

Remove-Item deletes one or more items. It can be used to delete many different types of items, including files, directories, registry keys, variables, aliases, and functions.


Remove a single folder:

PS C:\> remove-item .\foldertodelete -Force

Delete from the current directory (*) all files with a .doc file name extension and a name that does not include "1".

PS C:\> remove-item * -include *.doc -exclude *1*

Delete a file that is both hidden and read-only:

PS C:\> remove-item -path C:\Docs\hidden-RO-file.txt -force

Delete everything from a directory including the directory itself, without prompting the user. Also don’t throw an error if the directory does not exist:

PS C:\> remove-item .\foldertodelete -Force -Recurse -ErrorAction SilentlyContinue

Delete all items that include a dot (.) typically this will delete files and not folders, but this is not guaranteed, it is possible to create files without an extension and folder names that do contain a period:

PS C:\> remove-item C:\Docs\*.*

Delete all files in a folder and all its subfolders leaving the empty folders:

Get-ChildItem -Path 'C:\Docs\' -File -Recurse -Force | Remove-Item -WhatIf

# you could also skip specific filenames with the -Exclude option.

Delete only a folder called ss64:

PS C:\> remove-item ss64 | Where { $_.PSIsContainer }

Delete only a file called ss64:

PS C:\> remove-item ss64 | Where { ! $_.PSIsContainer }

Delete all of the CSV files in the current directory and all subdirectories recursively, because remove-item -recurse is faulty this makes use of get-childitem -recurse instead:

PS C:\> get-childitem * -include *.csv -recurse | remove-item

Delete all of the 'Thumbs.db' files in the directory C:\demo and all subdirectories recursively:

PS C:\> Get-ChildItem -Path C:\demo -Include Thumbs.db -Recurse -Force | Remove-Item -Force

Delete all of the 'Thumbs.db' files in the current directory and all subdirectories recursively (this will cope with very long pathnames):

PS C:\> Get-ChildItem -Path . -Include Thumbs.db -Recurse -Name -Force | Remove-Item -Force

Delete all content without deleting the folder itself:

PS C:\> Remove-Item "foldertokeep\*" -Force -Recurse

Delete ALL folders and files/subfolders below the current folder ( . ) including hidden files, note if you specify Get-ChildItem without the -recurse option, it will still offer to delete complete subfolders but you will get a Y/N prompt:

PS C:\> Get-ChildItem -Recurse -force . | Remove-Item -force -confirm:$false

Delete all EMPTY folders and files/subfolders below the current folder:

PS C:\> Get-ChildItem -Recurse . | where { $_.PSISContainer -and @( $_ | Get-ChildItem ).Count -eq 0 } | Remove-Item

Remove a variable called $myVar:

PS C:\> remove-item variable:myVar

Delete an alias named 'list':

PS C:\> Remove-item alias:list

Delete the 'demo' registry key and all of its subkeys and values:

PS C:\> remove-item hklm:\software\SS64\demo -recurse

“But over all things brooding slept, The quiet sense of something lost” ~ Alfred Tennyson

Related PowerShell Cmdlets

Clear-item - Remove content from a variable or an alias.
Get-item - Return an object that represents an item in a namespace.
invoke-item - Invoke an executable or open a file (START).
Move-item - Move an item from one location to another.
New-item ni Create a new item in a namespace.
Set-item - Set the value of a provider pathname.
Rename-item - Change the name of an existing item.
Delete an 'undeletable' registry key - Idera.
Windows CMD: CIPHER /w - Overwrite data in a directory (secure delete).
Equivalent bash command: rm - Remove files.

Copyright © 1999-2024
Some rights reserved