I am trying to use Powershell to check a list of names (NameList.xlsx) against a group of 7 folders (in TestLab\TestTest), which are as much as three deep. The name of the person will be in the filename, but will not be an exact match for any of them. Example: search name is Bailey, Bill Filename might be: Bailey, Bill AF; or Bailey, Bill IA 20220122; or 2800 ID Bill Bailey 2022
I need matching files to be moved to the destination folder (Powershell Moves).
What the below command does is move all 7 folders to the destination folder, then appears to search for the files where they used to be and gives:
Move-Item : Cannot find path because it does not exist.
It doesn't look like it checks the NameList.xlsx file at all because the names it gives the location error on are not on the NameList, but in the folders.
$file_list = Get-Content C:\Users\shansen\Documents\TestLab\TestTest\Namelist.xlsx
$search_folder = "C:\Users\shansen\Documents\TestLab\TestTest"
$destination_folder = "C:\Users\shansen\Documents\TestLab\TestTest2\Powershell Moves"
foreach ($file in $file_list) {
$file_to_move = Get-ChildItem -Path $search_folder -Filter $file -Recurse -ErrorAction SilentlyContinue -Force | % { $_.FullName}
if ($file_to_move) {
Move-Item $file_to_move $destination_folder
}
}