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](https://activedirectorytools.net/wp-content/uploads/2024/04/powershell-delete-all-files-in-folder.webp)
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](https://activedirectorytools.net/wp-content/uploads/2024/04/powershell-delete-all-files-in-folder-but-keep-directory.webp)
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](https://activedirectorytools.net/wp-content/uploads/2024/04/powershell-delete-all-files-in-folder-and-subfolders.webp)
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](https://activedirectorytools.net/wp-content/uploads/2024/04/powershell-delete-all-files-in-folder-except-pdf-file-1024x623.webp)
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.