Remove-Item

Delete the specified items.

Syntax
      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]

Key
   -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.

   -Recurse 
       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.
       As a workaround, try piping results of the `Get-ChildItem -Recurse` command to Remove-Item

   -Force
       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+

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

   -Confirm
       Prompt for confirmation before executing the command.

   -UseTransaction
       Include the command in the active transaction.

   CommonParameters:
       -Verbose, -Debug, -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
       -OutBuffer -OutVariable.

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

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.

Examples

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 folders that do:

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

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 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:

PS C:\> Get-ChildItem -Recurse -force . | Remove-Item -force -recurse

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

“It devoured my paper, it was a really good paper” ~ Ellen Feiss

Related:

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
Equivalent bash command: rm - Remove files


Copyright © SS64.com 1999-2017
Some rights reserved