script – Powershell Transfer files under multiple subfolders to another folder under same Document Library Sharepoint


I have a document library “Documents1” which has multiple folders (aa, bb,cc, dd, Redundant).

Trying to write a powershell script to move files placed in multiple sub folders to folder “redundant” under the same doc lib.

The files to be moved needs to be read from a .txt file which contains list of files names present under various sub folders.

$WebURL="test.com"
 $SourceLibrary ="/Documents1"
 $TargetLibrary = "/Documents1/Redundant"
 $filenames = Get-Content 'F:usersfilelist.txt' 

#Get Objects $Web = Get-SPWeb $WebURL
 $SourceFile = $Web.GetFile($SourceLibrary) 
$Targetfile = $Web.GetFolder($TargetLibrary)
 foreach ($file in $filenames) { 
Get-ChildItem -Path $SourceFile -Recurse | Where-Object -FilterScript {($_.name -match $file)} | Move-Item -Destination $Targetfile 
Write-Output "File moved " $file 
}