ProphetSe7en
ProphetSe7en

Reputation: 535

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

Upvotes: 42

Views: 120544

Answers (7)

Ryan Chau
Ryan Chau

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

shadowz1337
shadowz1337

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

woter324
woter324

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

user11431789
user11431789

Reputation: 679

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

works for me.

Upvotes: 67

Roger Hoem-Martinsen
Roger Hoem-Martinsen

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

Esperento57
Esperento57

Reputation: 17472

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
   }

}

Upvotes: 1

Mark Wragg
Mark Wragg

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

Related Questions