I am trying to get the below PowerShell script to work using Task Scheduler. The problem is that it wont delete any files.
When I run it manually it needs a confirma
Delete a files folder\subfolders on D:\FOLDER
(my example below), any files that older than 30 days.
Get-ChildItem -Path "D:\FOLDER\" -Recurse |? {($_.LastWriteTime -lt (Get-Date).AddDays(-30))} | Remove-Item -Recurse -Force -confirm:$false -Verbose
The -Force -Confirm:$false
guarantees that you don't have to press Y
or A
every time it deletes a file or folder. The -Verbose
displays what is being deleted.
You need to add -Confirm:$false
to the Remove-Item
command to override the default confirmation behaviour. Failing that, try adding -Force
.
In my opinion Remove-Item -Path "C:\Temp\FolderToDelete" -Confirm:$false -Force
should just work without any prompt. But it doesn't.
To delete the whole folder and everything in it without any prompt, I had to use GCI and go up a level. So instead of:
Get-ChildItem -Path "C:\Temp\FolderToDelete" | Remove-Item -Recurse -Confirm:$false -Force
Which deletes everything inside FolderToDelete, but not the parent folder.
To delete the parent folder and everything in it without a prompt, I did:
Get-ChildItem -Path "C:\Temp\" -Directory -Filter "FolderToDelete" | Remove-Item -Recurse -Confirm:$false -Force
Note the trailing '\' in -Path C:\Temp\
.
HTH
This worked for me:
Get-ChildItem -Path "FolderToDelete" -Directory -recurse | where {$_.LastWriteTime -le $(get-date).Affffdays(-7)} | Remove-Item -recurse -force
Remove-Item foldertodelete -Recurse -Force -Confirm:$false
works for me.
It says
Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue?
Try:
Remove-Item ./folderToDelete -Force -Recurse