禁止在Powershell中自动确认删除 [英] Suppress automatic confirmation of deletion in powershell

查看:73
本文介绍了禁止在Powershell中自动确认删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个静默脚本,该脚本删除早于14天的文件并删除空文件夹.删除文件的部分工作正常,但是删除文件夹的部分弹出确认窗口,无论我做什么来抑制它.这是我的代码:

I am trying to write a silent script that deletes files older than 14 days and removes empty folders. The part that deletes files work fine, but the part that deletes folders is popping up a confirmation window no matter what I do to suppress it. Here's my code:

$date=(get-date).AddDays(-14)
$ConfirmPreference="None"
$DebugPreference="SilentlyContinue"
$ErrorActionPreference="SilentlyContinue"
$ProgressPreference="SilentlyContinue"
$VerbosePreference="SilentlyContinue"
$WarningPreference="SilentlyContinue"
$OutputEncoding=[console]::OutputEncoding

function FullNuke ([string] $strPath) {
  Get-ChildItem -Path $strPath -Recurse -Force | Where-Object {!$_.PSIsContainer -and $_.LastAccessTime -lt $date} | Remove-Item -Force
  #The line below is the one that triggers the confirmation
  Get-ChildItem -Path $strPath -Recurse -Force | Where-Object {$_.PSIsContainer -and @(Get-ChildItem -LiteralPath $_.FullName -Recurse -Force | Where-Object {!$_.PSIsContainer}).Length -eq 0} | Remove-Item -Force
}

我找到的大多数答案都说要在我的最终Remove-Item命令中添加-Recurse,但这与我想要的相反.如果文件夹为空,我希望将其删除.如果不为空,则不希望将其删除.我不确定为什么非空文件夹甚至一开始就被捕获.

Most of the answers I have found say to add -Recurse to my final Remove-Item command, but that would be the opposite of what I want. If the folder is empty, I want it removed. If it is not empty, I do not want it removed. I'm not sure why non-empty folders are even being caught in the first place.

更新

经过很多挫折之后,我发现第二行正在以相反的顺序处理项目,因此需要确认.它还没有正确识别空文件夹,这也触发了确认.我最终使用了以下功能.

After much frustration, I discovered that the second line was processing items in reverse order, thus requiring confirmation. It was also not properly identifying empty folders, which also triggered confirmation. I ended up using the following function.

function FullNuke ([string] $strPath) {
  Get-ChildItem -Path $strPath -Recurse -Force | Where-Object {!$_.PSIsContainer} | Where-Object {$_.LastAccessTime -lt $date} | Remove-Item -Force
  Get-ChildItem -Path $strPath -Recurse -Force | Where-Object {$_.PSIsContainer} | Where-Object {@(Get-ChildItem -LiteralPath $_.FullName -Recurse -Force).Length -eq 0} | Remove-Item -Force
}

之所以把它放在这里是因为,尽管这是一种解决方案(它使我满意的是删除文件和文件夹),但这并不是对我所发布问题的答案.

I put it here because while it is a solution (it erases files and folders to my satisfaction), it is not an answer to my posted question.

推荐答案

从第一个 Get-ChildItem 删除 -Force 并添加 -Recurse -Confirm:$ false .这将起作用:

Remove -Force from first Get-ChildItem and add -Recurse and -Confirm:$false. This will work:

Get-ChildItem -Path $strPath -Recurse | 
    Where-Object {$_.PSIsContainer -and 
        @(Get-ChildItem -LiteralPath $_.FullName -Recurse -Force | 
            Where-Object {!$_.PSIsContainer}).Length -eq 0} | 
    Remove-Item -Force -Recurse -Confirm:$false

这篇关于禁止在Powershell中自动确认删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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