Reputation: 535
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 confirmation before deleting files.
Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want
to continue?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
How can I edit this script to delete files without any confirmation so I can run it using Task Scheduler?
#----- define parameters -----#
#----- get current date ----#
$Now = Get-Date
#----- define amount of days ----#
$Days = "10"
#----- define folder where files are located ----#
$TargetFolder = "D:\Shares\Downloads\TV\AutoDL"
#----- define extension ----#
$Extension = "*.*"
#----- define LastWriteTime parameter based on $Days ---#
$LastWrite = $Now.AddDays(-$Days)
#----- get files based on lastwrite filter and specified folder ---#
$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
foreach ($File in $Files)
{
if ($File -ne $NULL)
{
write-host "Deleting File $File" -ForegroundColor "DarkRed"
Remove-Item $File.FullName | out-null
}
else
{
Write-Host "No more files to delete!" -foregroundcolor "Green"
}
}
Upvotes: 42
Views: 120544
Reputation: 69
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.
Upvotes: 6
Reputation: 889
This worked for me:
Get-ChildItem -Path "FolderToDelete" -Directory -recurse | where {$_.LastWriteTime -le $(get-date).Adddays(-7)} | Remove-Item -recurse -force
Upvotes: 1
Reputation: 3080
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
Upvotes: 21
Reputation: 679
Remove-Item foldertodelete -Recurse -Force -Confirm:$false
works for me.
Upvotes: 67
Reputation: 109
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
Upvotes: 9
Reputation: 17472
You can simplify your script like it:
-file
with Get-Childitem
command$Now
variableif
must be out for check if no files-Force
to your Remove-Item
command'\*.*'
Code ratified:
$Days = "10"
#----- define folder where files are located ----#
$TargetFolder = "D:\Shares\Downloads\TV\AutoDL"
#----- define LastWriteTime parameter based on $Days ---#
$LastWrite = (Get-Date).AddDays(-$Days)
#----- get files based on lastwrite filter and specified folder ---#
$Files = Get-Childitem $TargetFolder -Recurse -file | Where LastWriteTime -le "$LastWrite"
if ($Files -eq $null)
{
Write-Host "No more files to delete!" -foregroundcolor "Green"
}
else
{
$Files | %{
write-host "Deleting File $_" -ForegroundColor "DarkRed"
Remove-Item $_.FullName -Force | out-null
}
}
Upvotes: 1
Reputation: 23355
You need to add -Confirm:$false
to the Remove-Item
command to override the default confirmation behaviour. Failing that, try adding -Force
.
Upvotes: 22