How Can I Delete Files with Powershell Without Confirmation?

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"
    }
}
2

7 Answers

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

works for me.

4

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

3

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

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

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.

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

This worked for me:

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

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest