How to Delete Contents of Folder in PowerShell?

It is easy to delete the contents of a folder using PowerShell. Do you have a similar requirement? In this blog post, we’ll explore different methods to delete the contents of a folder using PowerShell with complete scripts.

To delete the contents of a folder in PowerShell, use the Remove-Item cmdlet with the path to the folder and a wildcard character. For example, Remove-Item -Path “C:\Your\Target\Directory*” -Recurse will delete all files and subdirectories in the specified folder. Use the -Force parameter to suppress prompts and -Confirm:$false to avoid confirmation requests.

Method 1: Using Remove-Item

In PowerShell, you can use the Remove-Item cmdlet to delete files and folders from a directory. To delete all files within a folder, you can use the following PowerShell script:

Get-ChildItem -Path "C:\MyFolder\Folder-1\" -File | Remove-Item

This script lists all the files in the specified directory and pipes them to Remove-Item, which deletes them. Note that this command does not delete subdirectories or their contents.

You can see the output in the screenshot below after I executed the script using VS code.

Delete Contents of Folder in PowerShell

If you want to delete all files and subdirectories using PowerShell, you can add the -Recurse parameter:

Get-ChildItem -Path "C:\MyFolder\Folder-1\" -Recurse | Remove-Item

However, be cautious with the -Recurse parameter, as it will delete everything within the directory, including all nested files and folders.

Method 2: Using Remove-Item with -Include and -Exclude

If you want to delete specific types of files from the directory, then check out the below PowerShell script. You can use the -Include and -Exclude parameters with Remove-Item PowerShell cmdlet to delete specific files.

For example, to delete all .txt files in a directory, execute the below PowerShell script:

Get-ChildItem -Path "C:\MyFolder\" -Include *.txt | Remove-Item

To delete everything except .txt files from the directory using PowerShell, write the below script.

Get-ChildItem -Path "C:\MyFolder\" -Exclude *.txt | Remove-Item

Method 3: Using Remove-Item Directly

You can use Remove-Item directly with the -Path parameter and a wildcard to delete all contents within a folder using PowerShell.

Remove-Item -Path "C:\MyFolder\*" -Recurse

This command deletes all files and subdirectories in the specified path. The wildcard character * represents all items in the directory.

Method 4: Clear File Contents Without Deleting Files

If you want to clear the contents of files without deleting the files themselves, you can use the Clear-Content cmdlet. This is useful for log files or other scenarios where you need to preserve the file but not the data it contains.

Get-ChildItem -Path "C:\MyFolder\" -File | Clear-Content

This PowerShell script will iterate through each file in the directory and clear its content, leaving empty files behind.

Method 5: Delete Files Older Than a Certain Date

If you want to delete files that are older than a certain date using PowerShell. This can be accomplished by adding a date filter to the Get-ChildItem command:

$limit = (Get-Date).AddDays(-30)
Get-ChildItem -Path "C:\MyFolder\" -File | Where-Object { $_.LastWriteTime -lt $limit } | Remove-Item

This PowerShell script deletes files in the specified directory that were last written to more than 30 days ago.

Method 6: Delete Empty Folders

After removing files, you may be left with empty subdirectories. To remove these as well, you can use the following PowerShell script:

Get-ChildItem -Path "C:\MyFolder\" -Directory | Where-Object { $_.GetFileSystemInfos().Count -eq 0 } | Remove-Item

This will find and delete all empty subdirectories within the target directory.

Handling Errors and Confirmations

When deleting files, you might encounter errors or prompts for confirmation. To suppress these, you can use the -Force and -Confirm:$false parameters:

Remove-Item -Path "C:\MyFolder\*" -Recurse -Force -Confirm:$false

This command will forcefully delete the contents without prompting for confirmation.

Conclusion

I hope this tutorial will help you to delete contents of folder using PowerShell. We covered different scenarios like:

  • Delete content from a folder using Remove-Item
  • Delete contents from a directory using Remove-Item with -Include and -Exclude
  • Clear file contents without deleting files using PowerShell
  • Delete files older than a certain date from a folder using PowerShell
  • Delete empty folders from a directory using PowerShell

You may also like: