0

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
    }
}
  • 3
    Why are you reading an Excel document as if it was a plain text file? Doesnt make sense – Santiago Squarzon Jun 20 '22 at 16:55
  • Have a look at `Import-Excel` command from [ImportExcel](https://www.powershellgallery.com/packages/ImportExcel/7.1.0) module. – zett42 Jun 20 '22 at 20:24
  • See also: https://stackoverflow.com/q/22868031/7571258 – zett42 Jun 20 '22 at 20:30
  • Thanks for your help. I'm a rookie so I'm sorry if I didn't understand what you meant, but I converted the xlsx document to both a txt and csv document. The script does nothing when I do this, just returns to the C: prompt for both. So I guess it was checking the xlsx name list after all, just not executing as needed. Any other suggestions would be appreciated. This is the link where I got the script I modified, if that helps: https://stackoverflow.com/questions/45563748/move-files-from-one-folder-to-another-from-a-list-of-filenames-from-a-text-file – S. Hansen Jun 22 '22 at 15:01

0 Answers0