PowerShell Remove-Item Command Tutorial

The Remove-Item command or cmdlet is used to delete single or more items. Remote-Item command supports the removal of the different types of objects like files, folders, registry keys, variables, aliases, and functions.

Delete File

The Remove-Item command can be used to delete or remove a file. There are different ways to provide the file to removal but the most simple way is providing the file name as parameter. In the following example we delete file named “test.txt”.

Remove-Item test.txt

Also a file can be deleted by specifying the absolute or full path like below.

Remove-Item D:\backup\test.txt

Delete Directory

The Remove-Item can be also used to delete directories or folders. Like the removal of the file provide the directory name to delete. In the following example we will delete the directory or folder named test.

Remove-Item test

Also a file can be deleted by specifying the absolute or full path like below.

Remove-Item D:\backup\test

Delete Specific Files with Specific Extensions

The Remove-Item command supports the glob operator which can be used to delete specific files according to their names or extension. In the following example we will delete “*.txt” files .

Remove-Item *.txt

Alternatively we can specify different path or directory we want to delete specific file extensions.

Remove-Item D:\backup\*.txt

Delete Recursively

The Remove-Item command can be used to delete given diretory and its contents recursively. The -Recurse attributes is provided to delete folders and directories recursively.

Remove-Item -Recurse D:\backup

Delete Registry Item

The Remove-Item is a very powerful tool that can be also used to delete registry keys. The “HKLM” can be used to specify the registry key where the registry key path also specified like “HKLM:\Software\Windows\Key”.

Remote-Item HKLM:\Software\Windows\Key

Also related path and all of its subkeys can be recursively delete with the -Recurse attribute like below.

Remote-Item -Recurse HKLM:\Software\Windows\Key

Delete PowerShell Variable

Remove-Item can be also used to remove PowerShell variables. Even this may sound a bit interesting variables can be removed and then created again. This will make the working PowerShell environment clean and safe. In order to remove a PowerShell variable just provide the variable into the Remove-Item command. In the following example, we will remove the variable named $age.

Remove-Item $age

Alternatively we can remove the variable by using the -Name attribute like below which will be more structural and easy to read.

Remove-Item -Name $age

Delete PowerShell Alias

Similar to the removing the variable alias can be also removed with the Remove-Item. Just provide the alias name into the Remove-Item command like below.

Remove-Item myalias

Delete PowerShell Function

The Remove-Item can be also used to remove a defined PowerShell function. Just provide the function name in order to remove it from the current PowerShell environment.

Remove-Item myfunction()

Prompt Confirmation Before Delete

In some cases deleting files especially recursively can be very difficult and dangerous. We may need to confirm every deletion of the files. The -Confirm can be provided in order to create the confirmation of the file and folder removal with the Remove-Item command.

Remove-Item -Confirm

Leave a Comment