What does the PowerShell cmdlet <code>Remove-Item</code> do?
It deletes files, folders, or other items from a specified location.
Click to reveal answer
beginner
How do you remove a file named example.txt using Remove-Item?
Use the command: Remove-Item example.txt
Click to reveal answer
intermediate
What parameter do you use with Remove-Item to delete a folder and all its contents?
Use the -Recurse parameter to delete a folder and everything inside it.
Click to reveal answer
intermediate
How can you force deletion of a read-only file with Remove-Item?
Add the -Force parameter to override restrictions and delete the file.
Click to reveal answer
beginner
What happens if you try to remove a file that does not exist using Remove-Item without any error handling?
PowerShell will show an error saying the file was not found.
Click to reveal answer
Which parameter deletes all files and subfolders inside a folder with Remove-Item?
A-Recurse
B-Force
C-Path
D-Include
✗ Incorrect
The -Recurse parameter tells PowerShell to delete all contents inside the folder recursively.
What does the -Force parameter do in Remove-Item?
ADeletes hidden or read-only files
BDeletes only empty folders
CPrompts before deleting
DCopies files instead of deleting
✗ Incorrect
The -Force parameter allows deletion of hidden or read-only files that normally cannot be deleted.
How do you delete a single file named data.csv using Remove-Item?
ARemove-Item -Force
BRemove-Item -Recurse data.csv
CRemove-Item data.csv
DRemove-Item -Include data.csv
✗ Incorrect
Simply specify the file name as the path: Remove-Item data.csv.
What happens if you run Remove-Item on a folder without -Recurse and the folder is not empty?
AThe folder and contents are deleted
BThe folder is renamed
COnly files inside are deleted
DPowerShell shows an error and does not delete
✗ Incorrect
Without -Recurse, PowerShell will not delete a non-empty folder and will show an error.
Which command deletes all .log files in the current folder?
ARemove-Item -Recurse *.log
BRemove-Item *.log
CRemove-Item -Force *.txt
DRemove-Item -Include *.log
✗ Incorrect
Using a wildcard *.log deletes all files ending with .log in the current folder.
Explain how to safely delete a folder and all its contents using PowerShell's Remove-Item.
Think about deleting folders with files inside and how to override restrictions.
You got /4 concepts.
Describe what happens if you try to remove a file that does not exist using Remove-Item without error handling.
Consider what PowerShell does when the target is missing.
You got /3 concepts.
Practice
(1/5)
1. What does the PowerShell command Remove-Item do?
easy
A. Lists files or folders in a directory
B. Deletes files or folders you specify
C. Copies files or folders to another location
D. Creates new files or folders
Solution
Step 1: Understand the purpose of Remove-Item
The Remove-Item command is used to delete files or folders in PowerShell.
Step 2: Compare with other file commands
Creating, copying, or listing files are done by other commands like New-Item, Copy-Item, and Get-ChildItem.
Final Answer:
Deletes files or folders you specify -> Option B
Quick Check:
Remove-Item = Delete files/folders [OK]
Hint: Remove-Item means delete files or folders [OK]
Common Mistakes:
Confusing Remove-Item with Copy-Item
Thinking Remove-Item creates files
Assuming Remove-Item lists files
2. Which of the following is the correct syntax to delete a folder and all its contents using Remove-Item?
easy
A. Remove-Item -Path 'C:\Folder' -Force
B. Remove-Item -Path 'C:\Folder' -DeleteAll
C. Remove-Item -Path 'C:\Folder' -Recurse
D. Remove-Item -Path 'C:\Folder' -All
Solution
Step 1: Identify the parameter to delete folder contents
The -Recurse parameter tells Remove-Item to delete all contents inside a folder recursively.
Step 2: Check other options
-Force deletes hidden or read-only files but does not delete folder contents recursively. -DeleteAll and -All are not valid parameters.
Final Answer:
Remove-Item -Path 'C:\Folder' -Recurse -> Option C
Quick Check:
-Recurse deletes folder contents [OK]
Hint: Use -Recurse to delete folder and contents [OK]
Common Mistakes:
Using -Force instead of -Recurse to delete folder contents
Using invalid parameters like -DeleteAll
Forgetting to specify -Recurse for folders
3. What will be the output of this PowerShell command if the file secret.txt is hidden and read-only?
Remove-Item -Path 'C:\temp\secret.txt'
medium
A. File is renamed to secret.txt.old
B. File is deleted successfully
C. File is moved to Recycle Bin
D. Error: Access to the file is denied
Solution
Step 1: Understand default Remove-Item behavior on protected files
By default, Remove-Item cannot delete hidden or read-only files without the -Force parameter.
Step 2: Predict the command result
Since -Force is missing, PowerShell will throw an access denied error when trying to delete the protected file.
Final Answer:
Error: Access to the file is denied -> Option D
Quick Check:
Missing -Force causes access denied error [OK]
Hint: Use -Force to delete hidden or read-only files [OK]
Common Mistakes:
Assuming file deletes without -Force
Thinking Remove-Item moves files to Recycle Bin
Confusing rename with delete
4. You run this command:
Remove-Item -Path 'C:\temp\*' -Recurse
But it fails with an error saying some files are read-only. How can you fix it?
medium
A. Add the -Force parameter to the command
B. Remove the -Recurse parameter
C. Run the command as a different user
D. Use Remove-Item -Path 'C:\temp\*' -Delete
Solution
Step 1: Identify cause of failure
The error occurs because some files are read-only and Remove-Item needs -Force to delete them.
Step 2: Fix command by adding -Force
Adding -Force allows deletion of hidden or read-only files along with -Recurse for folder contents.
Final Answer:
Add the -Force parameter to the command -> Option A
Quick Check:
-Force deletes read-only files [OK]
Hint: Add -Force to delete read-only or hidden files [OK]
Common Mistakes:
Removing -Recurse instead of adding -Force
Using invalid parameter -Delete
Ignoring permission issues
5. You want to delete all files and folders inside C:\Projects, including hidden and read-only files, but keep the Projects folder itself. Which command should you use?
hard
A. Remove-Item -Path 'C:\Projects\*' -Recurse -Force
B. Remove-Item -Path 'C:\Projects' -Recurse -Force
C. Remove-Item -Path 'C:\Projects' -Force
D. Remove-Item -Path 'C:\Projects\*' -Force
Solution
Step 1: Understand the goal
You want to delete everything inside the folder but keep the folder itself intact.
Step 2: Choose correct path and parameters
Using 'C:\Projects\*' targets all contents inside the folder. Adding -Recurse deletes subfolders and contents. -Force deletes hidden/read-only files.
Step 3: Avoid deleting the folder itself
Using 'C:\Projects' as path deletes the folder and contents, which is not desired.
Final Answer:
Remove-Item -Path 'C:\Projects\*' -Recurse -Force -> Option A
Quick Check:
Use path with * plus -Recurse and -Force to keep folder [OK]
Hint: Use path with * plus -Recurse and -Force to keep folder [OK]
Common Mistakes:
Deleting the folder itself by omitting *
Forgetting -Force to delete hidden files
Not using -Recurse to delete subfolders
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.