44

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"
    }
}
1
  • 2
    Powershell be like, false needs a $ except when it don't. Commented May 14, 2021 at 17:42

7 Answers 7

70
Remove-Item foldertodelete -Recurse -Force -Confirm:$false

works for me.

Sign up to request clarification or add additional context in comments.

4 Comments

A gotcha... I believe the item/object is a folder/directory, that also contains files, then not including -Recurse while including -Confirm:$false will result in a confirmation. Take a closer look at the confirmation as it begins like The item at <some-folder-pathname> has children and the Recurse parameter was not specified. something about the missing -Recurse switch.
Why is it -confirm:$false here? When I test: try { Remove-Item .\bin -Confirm:False } catch{ } Does not confirm. try { Remove-Item .\bin -Confirm:$false } catch{ } does confirm. Powershell sure be shifty and weird.
@Warren P When you omit the $ PS throws a ParameterBindingException, which you catch in you line of code. And sure, if it throws, it doesn't ask for confirmation...
-Confrim:$false is misleading,... the default is false, the problem is not the -Confirm it is the -Recurse (only the -Recurse)
22

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

2 Comments

Agree with your -Recurse. For me Remove-Item was always asking the prompt cos I had * to end of path. Thanks.
Same as the other answers. the sentiment here is correct by using Get-ChildItem and piping it into Remove-Item but still... -Confrim:$false is misleading,... the default is false, the problem is not the -Confirm it is the -Recurse (only the -Recurse)
21

You need to add -Confirm:$false to the Remove-Item command to override the default confirmation behaviour. Failing that, try adding -Force.

3 Comments

This doesn't work for me for some reason when there are folders present.
Try this --> try { Remove-Item .\bin -Confirm:False } catch{ }
This answer is 100% wrong x2. it is neither the -Confirm swtich not the -Force switch... -Confrim:$false is misleading,... the default is false, the problem is not the -Confirm it is the -Recurse (only the -Recurse)
10

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

3 Comments

can you please elaborate this answer.
Yes. What I meant to say is that if the prompt says that you need to confirm since the recurse switch parameter is missing, you should add the recurse parameter.
THIS IS THE CORRECT ANSWER!, ironically it is the answer where someone has asked for elaboration, when it tells exactly what the situation is. additionally suggesting the -Force switch too. Great no mention of the -Confirm switch, so perfect answer! UPVOTED!
6

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.

2 Comments

Same as the other answers... -Confrim:$false is misleading,... the default is false, the problem is not the -Confirm it is the -Recurse (only the -Recurse) –
@TheArchitecta if the problem is the -Recurse, how would you suppress the confirmation prompt when using -recurse?
1

You can simplify your script like it:

  1. Use -file with Get-Childitem command
  2. Not necessary to have a $Now variable
  3. Use alias for your where (better visibility)
  4. Your if must be out for check if no files
  5. Add -Force to your Remove-Item command
  6. Extension are not necessary if you use '\*.*'

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
   }

}

2 Comments

Thank you, I`ll give it a try. Will this also delete empty subfolders? My last script left all the empty subfolders behind.
Not simple and not correct! Same as the other answers... -Confirm:$false is misleading,... the default is false, the problem is not the -Confirm it is the -Recurse (only the -Recurse) – –
1

This worked for me:

Get-ChildItem -Path "FolderToDelete" -Directory -recurse | where {$_.LastWriteTime -le $(get-date).Adddays(-7)} | Remove-Item -recurse -force

1 Comment

UP Voted, even though it could be given more content. because it is more correct than the rest of the answers with high scores.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.