How can I delete files with PowerShell without confirmation?

前端 未结 7 1912
暗喜
暗喜 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: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

提交回复
热议问题