Reputation: 11
I am trying to write a script that will search for:
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:
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.
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:
Here are the plain URLs:
oi42.tinypic.com/2nulmz4.jpg
oi43.tinypic.com/fwlxd0.jpg
oi42.tinypic.com/315hdw9.jpg
Upvotes: 1
Views: 12993
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