Forum Discussion

AngeloLobo_79's avatar
AngeloLobo_79
Copper Contributor
Feb 09, 2022

Script to Move Folder Files

Hi people. I have a share on a machine that has more than 1kk files and the manual copy process becomes unfeasible. I know the command to move these files to another folder, but I would like to move only the files up to 11/2021.

All files have the same extension.

 

Thanks.

 

    • hasanemresatilmis's avatar
      hasanemresatilmis
      Iron Contributor

      Hi Harm_Veenstra,

       

      Yes. I wanted it to be all in one script so that people who don't know the move command can benefit from it. 🙂 

  • Hi AngeloLobo_79,

     

    The PowerShell script you need is as follows. The script is for files with .txt extension before the specified date.

     

    $SourceFolder = "C:\OldFolder"
    $DestinationFolder = "C:\NewFolder"
    $Items = (Get-ChildItem -Path $SourceFolder -filter *.txt -Recurse | Where-Object LastWriteTime -lt '1/11/2021').Name
    foreach ($Item in $Items)
    {
        Try {
            Get-ChildItem -Path "$SourceFolder\$Item" | Move-Item -Destination $DestinationFolder
            Write-Host "$Item file moved to $DestinationFolder folder."
            }
        Catch
            {
            Write-Host "$Item file could not be moved to $DestinationFolder folder."
            }
    }

     

    Best Regards.

  • AngeloLobo_79 

     

    This searches for all bin files which have been saved before 01/11/2021 as example

    Get-ChildItem -path "c:\temp" -Filter *.bin  -Recurse | Where-Object Lastwritetime -lt '01/11/2021'

     

     

Resources