我怎样才能设置自动 [L] 不是全部?

How I can set automatically [L] Not to all?

我的问题是 powershell 7.2 switch [L] 不是全部

我找到了有关 -confirm 的信息,但对如何将自动 [L] 'Not to all' 设置为函数没有帮助。 我不会让用户输入。 -Confirm:$false, ConfirmPreference = "None" or 'Set-ExecutionPolicy Unrestricted' 没有帮助。 查询每次都来。您有关于如何将 [L] 设置为自动的想法/示例吗?

PowerShell 的确认提示并非设计用于以编程方式进行响应;相反:

  • 两者之一:用户应该以交互方式决定。

  • 或者:提示应该完全抑制暗示一个[A] Yes to All响应),这根据情况 需要:

    • -Confirm:$false,对于定义 确认影响(与 cmdlet 操作相关的潜在破坏性/风险的评级)的 cmdlet,可以由 common -Confirm parameter or the $ConfirmPreference preference variable, such as Remove-Item

      控制
    • -Force,对于原本 无条件 显示确认提示的 cmdlet,例如 Set-ExecutionPolicy.

      • 警告-Force 在其他 cmdlet 的上下文中通常具有不相关的含义,包括 Get-ChildItemRemove-Item,其中它用于在枚举/删除中包含隐藏项。
    • Remove-Item的情况下,具体来说 - 除了确认影响机制 - 你需要-Recurse来发出信号删除目录 及其所有内容的明确意图 .


在您的特定情况下,如果目标目录非空 ,则默认为 [L] No to All 相当于 根本不调用 Remove-Item :

# Assume $dir contains the literal directory path of interest.
# Note that -Force in the case of Get-ChildItem causes inclusion of hidden items.
if (Get-ChildItem -Force -LiteralPath $dir | Select-Object -First 1) {
  Write-Error "Cannot remove directory $dir, because it is not empty."
} 
else {
  Remove-Item -LiteralPath $dir
}

相反,如前所述,Remove-Item -Recurse -Confirm:$false -Force -LiteralPath $dir 会抑制确认提示,但悄悄地继续 删除(隐含[A] Yes to All)。