How can I delete files with PowerShell without confirmation?

前端 未结 7 1861
暗喜
暗喜 2021-01-11 11:56

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 confirma

相关标签:
7条回答
  • 2021-01-11 12:28

    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.

    0 讨论(0)
  • 2021-01-11 12:32

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

    0 讨论(0)
  • 2021-01-11 12:32

    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

    0 讨论(0)
  • This worked for me:

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

    0 讨论(0)
  • 2021-01-11 12:46
    Remove-Item foldertodelete -Recurse -Force -Confirm:$false
    

    works for me.

    0 讨论(0)
  • 2021-01-11 12:49

    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

    0 讨论(0)
提交回复
热议问题