TechWindows

How to remove multiple folders and files using PowerShell using a script

How to remove multiple folders and files using PowerShell using a script

To delete a list of folders using PowerShell, you can use the

Remove-Item

cmdlet. The

Remove-Item

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

Remove-Item

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

Remove-Item

cmdlet with the

-Force

and

-Recurse

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

$foldersToDelete

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

Remove-Item

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

.ps1

file (e.g.,

DeleteFolders.ps1

), and then run the script using PowerShell. The script will loop through each folder path in the

$foldersToDelete

array, check if the folder exists (

Test-Path

), and then delete it using the

Remove-Item

cmdlet with the

-Recurse

and

-Force

parameters.

Make sure to customize the

$foldersToDelete

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.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button