gpt4 book ai didi

powershell - 如何在没有确认的情况下使用PowerShell删除文件?

转载 作者:行者123 更新时间:2023-12-04 09:00:54 24 4
gpt4 key购买 nike

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

当我手动运行它时,需要先确认,然后再删除文件。

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"):

如何编辑此脚本以删除文件而无需任何确认,以便可以使用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"
}
}

最佳答案

您需要将-Confirm:$false添加到Remove-Item命令中,以覆盖默认的确认行为。如果失败,请尝试添加-Force

关于powershell - 如何在没有确认的情况下使用PowerShell删除文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43611350/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com