如何在没有确认的情况下使用 PowerShell 删除文件? [英] How can I delete files with PowerShell without confirmation?

查看:47
本文介绍了如何在没有确认的情况下使用 PowerShell 删除文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用任务计划程序使以下 PowerShell 脚本工作.问题是它不会删除任何文件.

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

推荐答案

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

对我有用.

这篇关于如何在没有确认的情况下使用 PowerShell 删除文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆