'Create and Compare Arrays From List of Files

I have a list of teams, and a folder with multiple files. What I'm trying to accomplish is to only send the files, together, that correspond to the correct team.

Example:

$TeamA: UserA,UserB,UserC
$TeamB: UserD,UserE,UserF

Files in the folder follow the following naming convention:

[directory]\UserA Errors Report 2022-02-15.xlsx
[directory]\UserB Errors Report 2022-02-15.xlsx
[directory]\UserC Errors Report 2022-02-15.xlsx

So I want to send the files that contain UserA/B/C in the name, to X, the files that contain UserD/E/F to Y, etc.

I imagine the best method is to save the file locations to an array, then check if the directories contain the username of members of Team A, attach those files. I am having trouble completing this specific task.

File Locations:

$FileNames = $LatestFiles.Name

My attempt at saving the "matching" results to a variable:

$Comparison = foreach ($member in $FileNames) {
    if ($TeamA | Where-Object { $_ -contains $FileNames}) {
        Write-Output $member
       #I only want to save the file names that match members of TeamA to the "$Comparison" variable.
    }
}

However the code above returns blank, so there's something I am doing wrong.

Please let me know if there's a much better approach.

I have a solution but it's not very elegant:

$test1 = $LatestFiles |
ForEach-Object {
    $fileName = $_.BaseName -split ' '
    Write-Output @{
        User = $fileName[0]
        Tester = $fileName[1]
        Date = $fileName[2]
        File = $_
    }
} 

$ArrayTest = $test1 | Where-Object User -In $TeamA 

$Target = $ArrayTest.File.Name | ForEach-Object { @{ Name = "C:\X\X\Documents\Error Reporting\$($LatestFolder.Name)\$($_)" } }

But this means for all other teams, I'll have to create variables to further store their locations as well. IT seems inefficient.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source