To delete a list of folders using PowerShell, you can use the
cmdlet. The
cmdlet is used to remove files and folders. If you want to delete multiple folders in one go, you can provide an array of folder paths to the
cmdlet.
Here’s the basic syntax to delete a list of folders:
You can use copy this to the txt file and save as .ps1 and once saved go to that path and run the script
$foldersToDelete = @( "C:\path\to\folder1", "C:\path\to\folder2", "C:\path\to\folder3" # Add more folder paths as needed ) Remove-Item $foldersToDelete -Recurse -Force
Explanation:
-
$foldersToDelete
is an array that contains the paths to the folders you want to delete.
- The
Remove-Item
cmdlet is used to remove items (folders/files) in PowerShell.
-
-Recurse
specifies that the cmdlet should delete the folders recursively, including all files and subfolders within them.
-
-Force
is used to suppress the confirmation prompts that ask you to confirm the deletion.
Please be cautious while using the
cmdlet with the
and
parameters, as it will delete the specified folders and their contents without any further confirmation. Make sure you double-check the folder paths in the
array to avoid unintended deletions. Always take a backup or confirm the list of folders you are about to delete before running the script.
Another way of doing the same with a differnet script
To delete multiple folders using a PowerShell script, you can create a script that includes the
cmdlet with the appropriate parameters. Here’s an example of how to do it:
# List of folders to delete $foldersToDelete = @( "C:\path\to\folder1", "C:\path\to\folder2", "C:\path\to\folder3" # Add more folder paths as needed ) # Loop through the array and delete each folder foreach ($folderPath in $foldersToDelete) { if (Test-Path -Path $folderPath -PathType Container) { Remove-Item -Path $folderPath -Recurse -Force Write-Host "Deleted folder: $folderPath" } else { Write-Host "Folder not found: $folderPath" } }
Save the above code as a
file (e.g.,
), and then run the script using PowerShell. The script will loop through each folder path in the
array, check if the folder exists (
), and then delete it using the
cmdlet with the
and
parameters.
Make sure to customize the
array with the paths of the folders you want to delete. When you run the script, it will delete the specified folders and their contents without any further confirmation. Always double-check the folder paths and take backups if needed to avoid unintended data loss.