Search code examples
powershellcopy-item

PowerShell Recursive copying and pause between each file is copied


I have the following script to recursive copy data and create a log file of the destination, could any assist please, I would like to pause for 10 seconds after each file is copied so each file allocated a different created time stamp.

$Logfile ='File_detaisl.csv'
$SourcePath = Read-Host 'Enter the full path containing the files to copy'
""
""
$TargetPath = Read-Host 'Enter the destination full path to copy the files'
""
#$str1FileName = "File_Details.csv"

Copy-Item -Path $SourcePath -Destination $TargetPath -recurse -Force

Get-ChildItem -Path $TargetPath -Recurse -Force -File  | Select-Object Name,DirectoryName,Length,CreationTime,LastWriteTime,@{N='MD5 Hash';E={(Get-FileHash -Algorithm MD5 $_.FullName).Hash}},@{N='SHA-1 Hash';E={(Get-FileHash -Algorithm SHA1 $_.FullName).Hash}} | Sort-Object -Property Name,DirectoryName | Export-Csv -Path $TargetPath$Logfile

Solution

  • Copy-Item has a -PassThru parameter that outputs each item that is currently processed. By piping to ForEach-Object you can add a delay after each file.

    Copy-Item -Path $SourcePath -Destination $TargetPath -recurse -Force -PassThru | 
        Where-Object PSIsContainer -eq $false |
        ForEach-Object { Start-Sleep 10 }
    

    The Where-Object is there to exclude folders from the ForEach-Object processing. For folder items the PSIsContainer property is $true and for files it is $false.