【问题标题】:Automatic confirmation of deletion in powershell在PowerShell中自动确认删除
【发布时间】:2011-06-12 19:51:28
【问题描述】:

我正在运行以下命令:

get-childitem C:\temp\ -exclude *.svn-base,".svn" -recurse | foreach ($_) {remove-item $_.fullname}

经常这样提示我:

Confirm
The item at C:\temp\f\a\d has children and the 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"): 

我怎样才能让它自动设置为“A”?

【问题讨论】:

    标签: scripting powershell windows-vista automation


    【解决方案1】:

    默认为:无提示。

    您可以使用-Confirm 启用它或使用-Confirm:$false禁用

    不过还是会提示,当目标:

    • 是一个目录
    • 并且不为空
    • 并且未指定-Recurse 参数。

    -Force 还需要删除隐藏和只读项等。

    总结一下:

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

    ...应该涵盖所有场景。

    【讨论】:

      【解决方案2】:

      添加 -confirm:$false 以禁止确认。

      【讨论】:

      • 这行不通。 -Confirm 是强制确认。关闭它只会运行正常的决策逻辑。
      • 你使用 -force -confirm:$false 吗?
      【解决方案3】:

      尝试在Remove-Item 上使用-Force 参数。

      【讨论】:

      • 请参阅下面的答案以获得对此问题的更全面的答案。
      【解决方案4】:

      在remove-item之后添加-recurse,-force参数也有助于删除隐藏文件 例如:

      gci C:\temp\ -排除 *.svn-base,".svn" -recurse | %{ri $_ -force -recurse}

      【讨论】:

      • -recurse 阻止确认删除非空文件夹。谢谢。
      【解决方案5】:
      Remove-Item .\foldertodelete -Force -Recurse
      

      【讨论】:

        【解决方案6】:

        您只需要在该行后面添加一个/A

        例子:

        get-childitem C:\temp\ -exclude *.svn-base,".svn" -recurse | foreach ($_) {remove-item $_.fullname} /a
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-15
          • 2016-01-04
          • 1970-01-01
          • 2014-05-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多