coding1
coding1

Reputation: 11

PowerShell script for deleting files and folders of specific criteria

I am trying to write a script that will search for:

  1. Folders with specific names.
  2. Files with specific extensions while
  3. Excluding certain directories from the search.

Once the search is performed, I'd like all the 1s and 2s to be deleted as well as the parent folders if they don't match 3. Here's what I have so far:

#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

So here are the issues I'm having:

  1. I can get the script to work. However, I'm not sure how to go about deleting the parent folder while protecting the excluded ones.

  2. The output to the file is not working. The file gets created, but it's blank. Why?

After looking at Get-ChildItem | Get-Member, I realized that the Parent property is specified by System.IO.DirectoryInfo Parent, so if I can add that to the list of items to be deleted then it should work.

The folder structure is below. To reiterate in a perhaps clearer way, I want to delete a folder:

  1. Whose name or content matches keyword(s)
  2. If the content matches keyword(s), but the folder's name does not > check to see if it's explicitly excluded e.g. C:\Windows, C:\Program Files etc. > If it is, leave alone. If it's not, delete the folder.**

Here are the plain URLs:

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

Upvotes: 1

Views: 12993

Answers (1)

Andrey Marchuk
Andrey Marchuk

Reputation: 13483

You can keep it simple:

$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)
    }
}

As you can see you can define what folders you need to exclude upfront and then sort all your items, filtering out directories that should be kept. You can then exclude also some paths, or extensions with Remove-Item:

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

Upvotes: 2

Related Questions