Home » How to Delete All Files in a Folder in PowerShell

How to Delete All Files in a Folder in PowerShell

To delete all files in a folder in PowerShell, you can use the Get-ChildItem and Remove-Item cmdlets.

The following methods show how to remove all files in a folder.

Method 1: Delete All files in a folder Using Remove-Item

Get-ChildItem -Path $directoryPath | Remove-Item -Force

This example deletes all files in a folder using the Remove-Item cmdlet in PowerShell.

Method 2: Delete All files in a folder but keep the main directory using Remove-Item

 Remove-Item -Path $directoryPath\*.* -Force

This example deletes all files in a folder but keeps the main folder as is by using the \*.* after the Path parameter in the Remove-Item cmdlet.

Method 3: Delete All files in the folder and subfolders

Remove-Item -Path $directoryPath -Recurse -Include *.*

This example removes all files in the folder and subfolders by using the Remove-Item cmdlet along with -Recurse and Include *.* parameters.

Method 4: Remove all files in the directory except

 Remove-Item -Path $directoryPath\* -Exclude *.pdf

This example removes all files in the directory except the file with extension .pdf by using the Remove-Item cmdlet along with the -Exclude parameter.

The following examples show how to use these methods.

Delete All files in a folder Using Remove-Item

To delete all files in a folder, you can use the Get-ChildItem and Remove-Item cmdlets.

The following example shows how to do it with syntax.

# Specify the directory path
$directoryPath = "C:\temp\log\apps log\"

# Delete all files in the directory
Get-ChildItem -Path $directoryPath | Remove-Item -Force

Output:

PowerShell delete all files in a folder
PowerShell delete all files in a folder

In this example, the Get-ChildItem cmdlet retrieves all files from the specified directory $directoryPath and pipes them to the Remove-Item command. The Remove-Item command uses the -Force parameter to forcefully remove all the files in a folder.

Delete All files in a folder but keep the main directory using Remove-Item

Another way to delete all files in a folder but not a folder is by using the Remove-Item cmdlet.

The following example shows how to do it with syntax.

# Specify the directory path
$directoryPath = "C:\temp\log\apps log\"

# Delete all files in the directory but keep main directory
Remove-Item -Path $directoryPath\*.* -Force

Output:

PowerShell delete all files but keep the main directory
PowerShell delete all files but keeps the main directory

In the above PowerShell script, the Remove-Item command uses the -Path parameter to specify the directory from which you want to delete all files but keep the main directory as is.

Delete All files in the folder and subfolders

You can delete all files in the folder and subfolders by using the Remove-Item cmdlet along with the Recurse and Include parameters.

The following example shows how to do it with syntax.

# Specify the directory path
$directoryPath = "C:\temp\log\apps log\"

# Delete all files in the directory and subdirectories
Remove-Item -Path $directoryPath -Recurse -Include *.*

Output:

PowerShell delete all files from folder and subfolders
PowerShell delete all files from folder and subfolders

In this example, the Remove-Item cmdlet uses the -Path parameter to specify the directory name $directoryPath from where you want to remove all files, Recurse and Include*.” parameters to recursively delete all files from the folder and subfolders.

Remove all files in the directory except

You can remove all files in the directory except the specified criteria by using the Remove-Item cmdlet along with the Exclude parameter.

The following example shows how to do it with syntax.

# Specify the directory path
$directoryPath = "C:\temp\log\apps log\"

# Delete all files in the directory except the pdf file
Remove-Item -Path $directoryPath\* -Exclude *.pdf

Output:

PowerShell delete all files except pdf file
PowerShell delete all files except pdf file

In the above PowerShell script, the Remove-Item cmdlet uses the Path parameter to specify the directory path $directoryPath from where you want to delete all files excluding *.pdf files with the Exclude *.pdf parameter.

Conclusion

I hope the above article on how to delete multiple files in a folder using PowerShell is helpful to you.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.