【问题标题】:PowerShell script for deleting files and folders of specific criteria用于删除特定条件的文件和文件夹的 PowerShell 脚本
【发布时间】:2025-12-28 09:05:06
【问题描述】:

我正在尝试编写一个脚本来搜索:

  1. 具有特定名称的文件夹。
  2. 具有特定扩展名的文件而
  3. 从搜索中排除某些目录。

执行搜索后,我希望删除所有 1 和 2 以及父文件夹(如果它们与 3 不匹配)。这是我目前所拥有的:

#Define list of computers to search for the files and folders in question
$ComputerList = Get-Content -Path "C:\computers.txt"

#Define a function for searching files and folders based on criteria:
function search {
    PROCESS {
        $srcFolder ="C:\test"
        Get-ChildItem $srcFolder -ErrorAction SilentlyContinue  -recurse -Force | Where-Object {`
            $_.Name -eq “keyword1” -or`
            $_.Name -eq “keyword2” -or`

              -and $_.fullname -notmatch 'C:\\Windows'`
              -and $_.fullname -notmatch 'C:\\Program Files'
        } | foreach-object -process { _.FullName }
    }
}

foreach ($strComputer in $ComputerList)
{
    #Perform the search function on all the computers listed
    foreach ($objItem in $colItems)
    {
        write-host         "-------------------------$strComputer ----------------" -foregroundcolor "red"
        write-host         "                      Files Found                                                    " -foregroundcolor "yellow" -backgroundcolor "black"
             $ComputerList | search

        "Number of files and folders found: " +($ComputerList | search | Measure-Object | Select-Object -ExpandProperty Count)
        write-host "------------------------------------------------------------------" -foregroundcolor "red"
    }

    if (($ComputerList | search).count -lt 1) {
        write-host "No files found to delete"
    }
    else {
        #Prompt if you want to delete the files
        write-host         "Do you want to delete these files?" -foregroundcolor "yellow" -backgroundcolor "black"
        $ComputerList | search | Remove-Item -Force -confirm
    }
}
Out-File -FilePath C:\results.txt

以下是我遇到的问题:

  1. 我可以让脚本工作。但是,我不确定如何在保护排除的文件夹的同时删除父文件夹。

  2. 文件的输出不起作用。该文件已创建,但它是空白的。为什么?

查看Get-ChildItem | Get-Member 后,我意识到 Parent 属性是由 System.IO.DirectoryInfo Parent 指定的,所以如果我可以将其添加到要删除的项目列表中,那么它应该可以工作。

文件夹结构如下。为了更清楚地重申,我想删除一个文件夹:

  1. 其名称或内容与关键字匹配
  2. 如果内容与关键字匹配,但文件夹名称不匹配 > 检查它是否被明确排除,例如C:\Windows、C:\Program Files 等 > 如果是,请不要理会。如果不是,请删除该文件夹。**

以下是纯 URL:

oi42.tinypic.com/2nulmz4.jpg
oi43.tinypic.com/fwlxd0.jpg
oi42.tinypic.com/315hdw9.jpg

【问题讨论】:

    标签: powershell get-childitem


    【解决方案1】:

    你可以保持简单:

    $folder = "C:\pst"
    $excludedFolders = "c:\Windows", "c:\PST\new"
    
    $itemsFromFolder = get-childitem $folder -recurse
    $itemsToDelete = New-Object System.Collections.Generic.List[string]
    
    foreach ($item in $itemsFromFolder)
    {
        $exclude = $false
        foreach ($exclusion in $excludedFolders)
        {
            if ($item.FullName.ToLower().StartsWith($exclusion.ToLower()))
            {
                $exclude = $true
            }
        }
        if (!$exclude)
        {
            $itemsToDelete.Add($item.FullName)
        }
    }
    

    如您所见,您可以预先定义需要排除的文件夹,然后对所有项目进行排序,过滤掉应该保留的目录。然后,您还可以使用 Remove-Item 排除一些路径或扩展:

    Remove-Item c:\scripts\* -include *.txt -exclude *test*
    

    【讨论】:

    • 感谢您的意见。如果父项包含包含指定关键字的文件夹或文件,我也希望删除父项。
    • 换句话说,只要它们与我排除的路径(即 Windows 文件夹等)不匹配,我都想删除它们。
    • 再次感谢您。如果您不知道他们的名字,恐怕这不包括删除父文件夹。用户将创建包含由上述关键字搜索的文件/其他文件夹的文件夹。我无法知道父文件夹的名称,只知道它们的内容,所以我无法明确包含它们。是否有一些通用的方法来包含父文件夹,例如“../”?