Change a position based script to move folders only if the folder name exists in the text file line

Solution 1:

You may need to add a trailing slash to your folder names because you're joining them again. Try out writing your values back to the console and see if they are actually correct, plus you can remove some of your path checking since you're using -Force already:

$csvpath = 'C:\temp\temp.csv'
$invalid = "[{0}]" -f [RegEx]::Escape(([IO.Path]::GetInvalidFileNameChars() -join ''))
$sourcePath = 'C:\temp\'

Import-Csv C:\temp\temp.csv -Header Title,FileName,Link -Delimiter ';' | 
  Group-Object Title | 
  Foreach {

    # I prefer to add a trailing slash to folder names
    $TargetFolder = Join-Path -Path $sourcePath -ChildPath (($_.Name -replace $invalid)+'\')

    # We don't have to create the new folders, because -Force will create them for us
    Foreach ($fileName in $_.Group.FileName) {
      $ValidFileName = $filename -replace $invalid
      $targetFile = Join-Path -Path $sourcePath -ChildPath $fileName

      # Write your values to the console - Make sure the folder is what it should be
      Write-Output "Moving '$targetFile' to '$TargetFolder'"
      Move-Item $targetFile $TargetFolder -Force -WhatIf
    }
  }

So the output looks like:

Moving 'C:\temp\(1959 10) Showcase Presents n  22' to 'C:\temp\Lanterna Verde - Le Prime Storie\'
What if: Performing the operation "Move File" on target "Item: C:\temp\(1959 10) Showcase Presents n  22 Destination: C:\temp\Lanterna Verde - Le Prime Storie\".