Move Some files from different locations

Hi everyone, here is the challenge I’ve been struggling with.
A user wants to move more than 26.000 files from 3 different network shared folders to another location.
The point I’ve been stuck is that they don’t want all the files to be copied, just some of them, from just some of the subfolders…

\server\shared folder\MainFolder1\subfolder1\filename.pdf
\server\shared folder\MainFolder1\subfolder2\filename.xls
\server\shared folder\MainFolder2\subfolder1\filename.pdf
\server\shared folder\MainFolder2\subfolder1\filename2.pdf

I couldn’t find a logic to copy just some of the files and how to make PowerShell “reads” from different locations.

get-help Move-Item

:slight_smile: Thanks, Dan. That was actually my first stop. Unfortunately there is no example like the one I need.


Do you have a list of known filenames that will need moving, regardless of what folder/subfolder they reside in?

Define your filenames in an array.
Define your file shares in a different array.

Iterate through your file shares, and for each file share do:

get-childitem -recurse on the root of the share you’re working with

Check to see if the filename you’re looking at is contained in the filename array. If it is, copy it somewhere.

Something along these lines

$Filenames = @("Filename1.xls","FileName2.txt","Filename3.jpg")
$Fileshares = @("\\server1\sharename","server2\sharename")
$DestinationPath = "\\DestinationServer\Sharename"

ForEach ($Fileshare in $Fileshares) {
        $Files = Get-ChildItem $Fileshare | where {-not $_.PSIsContainer}
        ForEach ($File in $Files) {
            If $Filenames.Contains($File.Name) {
                Copy-Item $File.FullName $DestinationPath

So, I was getting at the same direction.
I was getting something like this:

$shareddrive_path = "\\server\filseshare\"
$dest_path = "\\server\fileshare\"
$files = Get-Content C:\files.txt

foreach ($file in $files){
      Get-ChildItem -Recurse -Force $sharedrive_path -ErrorAction SilentlyContinue | Where-Object { ($_.PSIsContainer -eq $false) -and  ( $_.Name -like "*$file*") } | Copy-Item -Destination $dest_path -Container -Confirm 

The first line on your example, would be possible to use Get-Content? The reason is that the file list is huge, 26.000 files.

I’ll give a shot using a block of files and let you know.

Yes, but with an array that large you really want to put it in an ArrayList() instead of a standard powershell array.

$myArray = New-Object System.Collections.ArrayList
[void] $myArray.Add(Get-Content $files)

Unfortunately is not working.
So, explaining a little bit better.

I have the source_path like this:

I have to move 26000 files from source_a to target_b;

They request to move the files and try to preserve this structure:

Would be possible?