Forum Discussion
Script to delete automatically all incoming files in a folder.
Employee receives daily orders in excel files. If he completes the order, the file is moved to the folder "Completed orders". A PowerShell script is needed to delete automatically the files in the folder "Completed orders". Please help!
Hi,
This script will delete all excel in Completed orders.
Note the remove item is being remarked.
Also removing -recurse parameter will not check sub folders.Clear-Host # Extract List of XLSX Files $xlsxLocation = "C:\Completed orders\*.xlsx" $xlsxFiles = Get-ChildItem -Path $xlsxLocation -Force | Sort-Object LastWriteTime # List every XLSX Files foreach($xlsxFile in $xlsxFiles){ Write-Host $xlsxFile.Fullname # Remove-Item -Path $xlsxFile.Fullname }
- oliwer_sundgrenSteel Contributor
HelloAtanasM
This would be a rather simple script to set up.
See my example below that I put together for you
cd "C:\Users\User1\Folder\Completed Orders" $Files = Get-Childitem | Where-Object {$_.Name -like "*.xlsx"} Remove-Item $Files
These three simple lines of code will do three things
1: Direct the script to the folder "Completed orders"
2: Get all files in that folder that ends with ".xlsx" (In order words, gets all the Excel files in the folder) and saves it to a variable called $Files
3: Delete all the found files from step 2
Is this what you were looking for? You would need to set this up in a form of scheduled task for it to automatically run and clean the folder every hour or similar.
Let me know if this helped you or if you need further assistance 🙂
Kind Regards
Oliwer Sundgren- AtanasMIron ContributorHi @Oliwer Sundgren on which row should be the scheduled task if I wan to add it there?
- Mehar7100Copper Contributor
Sir,
Hope you will be fine
I have a folder Named: Production shared with my users.
I want to clean the folder. For example, any kind of file or data in this folder is to be deleted after 2 days or after 8 hours.
Please guide me on how to write a script for this.
Your given script is working with a specific format like .txt or xlsx.
thanks