I’ve been banging my head on this one so any help is appreciated.
Desired Outcome: Automate the deletion of files that don’t contain specific text in the filename.
Strategy: Create an array of filenames in a folder. Create an array of numbers from a text file. Identify filenames that contain no instance of the numbers from the text file. Delete those files.
Code so far:
# https://stackoverflow.com/questions/18616581/how-to-properly-filter-multiple-strings-in-a-powershell-copy-script # https://social.technet.microsoft.com/Forums/en-US/cdab4808-eb91-4df3-841b-92075a6fa0fb/creating-an-array-from-the-getchilditem-cmdlet?forum=ITCG # https://stackoverflow.com/questions/13998777/storing-directory-folder-names-into-array-powershell # Put the policy files into an array called "Files" $PolicyPath = "C:\ProgramData\LANDesk\Policies" #[array]$Files = @(Get-ChildItem -Path $PolicyPath\* -Include *.stat, *.xml | Select-Object -ExpandProperty fullname) [array]$Files = @(Get-ChildItem -Path $PolicyPath\* -Include *.stat, *.xml | Foreach-Object {$_.Name}) # https://sharepoint.stackexchange.com/questions/104056/contents-of-file-into-an-array-powershell # Put the list of active tasks into an array called "ActiveTasks" $ActiveTasksPath = "\\server\share" $ActiveTasks = [string[]](Get-Content -Path $ActiveTasksPath\ActiveTasks.txt) # Testing matching $ActiveTasks -match ($Files -join "|") $ActiveTasks -match $Files[135] | Foreach-object { "$_ should be kept" }
The result of the matching above turns out all the numbers in $ActiveTasks.
I’ve tried other comparison and they didn’t work either.
Thanks for reading my post!