【问题标题】:Powershell script deleting files despite -Exclude switch尽管 -Exclude 开关,Powershell 脚本仍删除文件
【发布时间】:2018-02-17 04:44:13
【问题描述】:

我有以下脚本,我试图删除除最后两个之外的所有 SQL .bak 文件。当我运行它时,它会清除文件夹中的所有内容。 -Exclude 是否不适用于数组值?

$excludefile=get-childitem D:\TempDB | sort lastwritetime | select-object -Last 2 | select-object -Property Name | select-object -expandproperty Name    

foreach ($element in $excludefile)
{
    $element
    remove-item -Path D:\TempDB -Exclude ($element) -Force 
}

【问题讨论】:

    标签: powershell


    【解决方案1】:

    这是你要找的吗?

    Get-ChildItem D:\TempDB |
      Sort-Object LastWriteTime -Descending |
      Select-Object -Skip 2 |
      Remove-Item -WhatIf
    

    当然,如果你需要,你可以删除-WhatIf

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-21
      • 2018-10-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多