Script to Move Folder Files

New Contributor

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.

 

4 Replies

@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'

 

 

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 Did our answers help ? @hasanemresatilmis included the file move part, I didn't because you said you already had that part ;) 

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. :)