Question

I wanted to know if there's an easy way to log all successful transferred items to a .txt file. I would like to create the .txt file and list all the files that are being transferred and updating it afterwards if it is used again (like on robocopy / log+)?

Current:

$loc1 = "C:\Users\user1\Documents\test2\*"

$loc2 = "C:\Users\user1\Documents\test2" 

Move-Item $loc1 $loc2 -force
Was it helpful?

Solution

Use -PassThru at the end of Move-Item. It will return a FileInfo object (same as returned by Get-ChildItem) for each item moved:

$itemsMoved = Move-Item test.* E:\Scratch\t2 -PassThru
$itemsMoved | Select-Object FullName | Add-content c:\temp\copy.log

OTHER TIPS

some options :

use the -verbose switch

if you have powershell V3 you can use the 'redirect everything' operator *>

Move-Item $loc1 $loc2 -force -Verbose *> c:\temp\copy.log

you can also use start-transcript

Start-Transcript -Path C:\temp\cp.log
Move-Item $loc1 $loc2 -force -Verbose
Stop-Transcript

or use a foreach-object

$loc1 |%{
    cp $_.FullName $loc2;
    if ($? -eq $true){ #copy ok
        echo "file copied : $($_.name)">>c:\temp\copy.log 
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top