Reputation: 159
Basically my goal is copy content of a folder to another folder with exclusion one name and also log everything what has been copied.
I'm stock on logging Get-ChildItem command combined with Pipeline --> Copy-Item
Bellow command will put to log file all data from command Get-ChildItem:
$SourcePath = "D:\TEST"
$DestPath = "C:\TEST"
$LogDetailFile = "C:\Temp\CopyDetail.log"
$Exclude = "!_Archive_!"
Get-ChildItem $SourcePath -Recurse | Where {$_.FullName -notmatch $Exclude} |
Select FullName | Add-Content $LogDetailFile
When I extra add with next Pipeline command to copy them to $DestPath it wont work:
Get-ChildItem $SourcePath -Recurse | Where {$_.FullName -notmatch $Exclude} |
Copy-Item -Destination {Join-Path $DestPath $_.FullName.Substring($SourcePath.length)} |
Add-Content $LogFile
When I done it without logging options, then everything works fine and whole data is copied:
Get-ChildItem $SourcePath -Recurse | Where {$_.FullName -notmatch $Exclude} |
Copy-Item -Destination {Join-Path $DestPath $_.FullName.Substring($SourcePath.length)}
I tried already switch pipelines between but it don't work. What I'm missing here? How to copy everything from one Folder to another and log all copied items to logfile?
Right now If I want to have 2 things - logging and copy those items I need to run 2 commands, just want to have it in one command.
Upvotes: 1
Views: 779
Reputation: 60668
ForEach-Object
can help you here, the reason why combining both breaks is because neither of those cmdlets (Add-Content
and Copy-Item
) produce output so there is nothing to pipe to.
$SourcePath = 'D:\TEST'
$DestPath = 'C:\TEST'
$LogDetailFile = 'C:\Temp\CopyDetail.log'
$Exclude = '!_Archive_!'
Get-ChildItem $SourcePath -Recurse | ForEach-Object {
if($_.FullName -notmatch $Exclude) {
$_ | Copy-Item -Destination { Join-Path $DestPath $_.FullName.Substring($SourcePath.Length) }
$_ | Select-Object FullName # Probably need `-ExpandProperty` here
}
} | Add-Content $LogDetailFile
Upvotes: 1