35

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"
    }
}
Mark Wragg
  • 22,105
  • 7
  • 39
  • 68
ProphetSe7en
  • 465
  • 1
  • 4
  • 4

7 Answers7

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

works for me.

double-beep
  • 5,031
  • 17
  • 33
  • 41
user11431789
  • 549
  • 4
  • 2
  • 5
    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 has children and the Recurse parameter was not specified.` something about the missing `-Recurse` switch. – Adam Cox Jan 08 '20 at 21:34
  • 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 May 14 '21 at 17:43
  • @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... – Valerij Dobler Apr 29 '22 at 13:01
  • 2
    -Confrim:$false is misleading,... the default is false, the problem is not the -Confirm it is the -Recurse (only the -Recurse) – TheArchitecta Jun 16 '22 at 06:07
21

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

Mark Wragg
  • 22,105
  • 7
  • 39
  • 68
  • 1
    This doesn't work for me for some reason when there are folders present. – Alex Kwitny Dec 11 '20 at 16:02
  • Try this --> try { Remove-Item .\bin -Confirm:False } catch{ } – Warren P May 14 '21 at 17:44
  • 1
    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) – TheArchitecta Jun 16 '22 at 06:08
18

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

woter324
  • 2,608
  • 5
  • 27
  • 47
  • 1
    Agree with your -Recurse. For me Remove-Item was always asking the prompt cos I had * to end of path. Thanks. – Adithya Ranganath Jun 08 '18 at 02:55
  • 1
    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) – TheArchitecta Jun 16 '22 at 06:10
7

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

  • 1
    can you please elaborate this answer. – Shanteshwar Inde Apr 16 '19 at 12:02
  • 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. – Roger Hoem-Martinsen Apr 18 '19 at 12:43
  • 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! – TheArchitecta Jun 16 '22 at 06:13
5

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.

B--rian
  • 5,578
  • 10
  • 38
  • 89
Ryan Chau
  • 59
  • 1
  • 1
  • 1
    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 Jun 16 '22 at 06:11
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
   }

}
henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
Esperento57
  • 16,521
  • 3
  • 39
  • 45
  • Thank you, I`ll give it a try. Will this also delete empty subfolders? My last script left all the empty subfolders behind. – ProphetSe7en Apr 25 '17 at 22:39
  • 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) – – – TheArchitecta Jun 16 '22 at 06:17
0

This worked for me:

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

shadowz1337
  • 710
  • 1
  • 8
  • 21
  • UP Voted, even though it could be given more content. because it is more correct than the rest of the answers with high scores. – TheArchitecta Jun 16 '22 at 06:20