0
0
PowerShellscripting~5 mins

Remove-Item in PowerShell

Choose your learning style9 modes available
Introduction
Remove-Item deletes files or folders you no longer need, helping keep your computer tidy.
You want to delete old log files to free up space.
You need to remove temporary files created by a program.
You want to delete a folder and all its contents.
You want to clean up files after a script finishes running.
Syntax
PowerShell
Remove-Item -Path <string> [-Recurse] [-Force]
Use -Path to specify the file or folder to delete.
Add -Recurse to delete all items inside a folder.
Use -Force to delete hidden or read-only files.
Examples
Deletes a single file named file.txt in the C:\temp folder.
PowerShell
Remove-Item -Path "C:\temp\file.txt"
Deletes the folder oldfolder and everything inside it.
PowerShell
Remove-Item -Path "C:\temp\oldfolder" -Recurse
Deletes a hidden or read-only file named hiddenfile.txt.
PowerShell
Remove-Item -Path "C:\temp\hiddenfile.txt" -Force
Sample Program
This script creates a file, confirms creation, deletes it, then confirms deletion.
PowerShell
New-Item -Path "C:\temp\testfile.txt" -ItemType File -Force
Write-Output "File created."
Remove-Item -Path "C:\temp\testfile.txt"
Write-Output "File deleted."
OutputSuccess
Important Notes
Be careful: Remove-Item deletes files permanently without sending them to the recycle bin.
Use -WhatIf to see what would be deleted without actually deleting anything.
If you get an error, check if the file is in use or if you have permission to delete it.
Summary
Remove-Item deletes files or folders you specify.
Use -Recurse to delete folders with contents.
Use -Force to delete hidden or protected files.