PowerShell Remove-Item Cmdlet
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed New-Item cmdlet in PowerShell.
https://cloudaffaire.com/powershell-new-item-cmdlet/
In this blog post, we will discuss Remove-Item cmdlet in PowerShell. You can use Remove-Item cmdlet to delete files and directories in PowerShell. Remove-Item cmdlet can also be used to delete symbolic links, hard links, registry keys etc by assigning appropriate value to “ItemType” argument.
Remove-Item Cmdlet Syntax:
1 2 3 4 5 6 7 8 9 10 11 12 |
## Remove-Item ## [-Path] ## [-Filter ## [-Include ## [-Exclude ## [-Recurse] ## [-Force] ## [-Credential ## [-WhatIf] ## [-Confirm] ## [-Stream ## [ |
Remove-Item Cmdlet Argument List:
- –Confirm: Prompts you for confirmation before running the cmdlet.
- –Credential: To impersonate another user or elevate your credentials.
- –Exclude: Specifies, as a string array, an item or items that this cmdlet excludes in the operation. The value of this parameter qualifies the Path parameter.
- –Filter: Specifies a filter to qualify the Path parameter. The FileSystem provider is the only installed PowerShell provider that supports the use of filters.
- –Force: Forces the cmdlet to remove items that cannot otherwise be changed, such as hidden or read-only files or read-only aliases or variables.
- –Include: Specifies, as a string array, an item or items that this cmdlet includes in the operation. The value of this parameter qualifies the Path parameter.
- –LiteralPath: Specifies a path to one or more locations. The value of LiteralPath is used exactly as it is typed. No characters are interpreted as wildcards.
- –Path: Specifies a path of the items being removed. Wildcard characters are permitted.
- –Recurse: Indicates that this cmdlet deletes the items in the specified locations and in all child items of the locations.
- –Stream: The Stream parameter is a dynamic parameter that the FileSystem provider adds to Remove-Item. This parameter works only in file system drives.
- –UseTransaction: Includes the command in the active transaction. This parameter is valid only when a transaction is in progress.
- –WhatIf: Shows what would happen if the cmdlet runs. The cmdlet is not run.
PowerShell Remove-Item Cmdlet:
Delete A New Directory In PowerShell:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
####################################### ## PowerShell | Cmdlet | Remove-Item ## ####################################### ## PowerShell Latest Version (5) ## create some directories and files for this demo New-Item -ItemType Directory -Path C:\MyDir\ New-Item -ItemType Directory -Path C:\MyDir\MyDir1\MySubDir1\MySubDir2 New-Item -ItemType Directory -Path "C:\MyDir\MyDir2","C:\MyDir\MyDir3","C:\MyDir\MyDir4" New-Item -ItemType File -Path "C:\MyDir\MyFile1","C:\MyDir\MyFile2","C:\MyDir\MyFile3" ## delete a directory in PowerShell Remove-Item -Path C:\MyDir\MyDir4 |
Delete A Directory And Sub-Directories Recursively In PowerShell:
1 2 3 |
## delete a directory and subdirectories recursively in PowerShell Remove-Item -Path C:\MyDir\MyDir1 -Recurse |
Delete Multiple Directories At Once In PowerShell:
1 2 3 |
## delete multiple directories in PowerShell Remove-Item -Path C:\MyDir\MyDir* |
Delete A File In PowerShell:
1 2 3 |
## delete a file in PowerShell Remove-Item -Path C:\MyDir\MyFile1 |
Delete Multiple Files At Once In PowerShell:
1 2 3 4 5 6 7 |
## delete multiple files in PowerShell Remove-Item -Path C:\MyDir\MyFile* ## delete the directory created for this demo Remove-Item C:\MyDir |
Hope you have enjoyed this article. In the next blog post, we will discuss Copy-Item cmdlet in PowerShell.
To get more details on PowerShell, kindly follow below official documentation