Move Some files from different locations

This topic contains 6 replies, has 3 voices, and was last updated by  Luiz Fabiano Lucena 1 year, 10 months ago.

  • Author
  • #46267

    Luiz Fabiano Lucena

    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.

  • #46271

    Dan Potter

    get-help Move-Item

  • #46304

    Luiz Fabiano Lucena

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

  • #46350

    Ian Kelso


    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
  • #46360

    Luiz Fabiano Lucena

    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.

  • #46366

    Ian Kelso

    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)
  • #46625

    Luiz Fabiano Lucena

    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?

You must be logged in to reply to this topic.