I am trying to copy files from local machines to remote location. When i run the script it will check folder exist on remote location. If it is not created then it will create a directory. Once directory created it will copy all files from source location.
My script is working fine for that but i want output on screen what files are being copied.
Below are the code that is working fine to copy the files but it is not showing file names being copied.
$machine = $env:COMPUTERNAME
$dest= "\\192.168.1.5\d$\test\"
$source = "C:\windows\logs\"
$newPath = Join-Path $dest -childpath $machine
if(!(Test-Path -Path $newPath )){
New-Item $newPath -type directory
foreach ($file in $source)
{
write-host "Copying Files: " $file -foregroundcolor DarkGreen -backgroundcolor white
Copy-Item $file -Destination $newPath -force
}
}else{
foreach ($file in $source)
{
write-host "Copying Files: " $file -foregroundcolor DarkGreen -backgroundcolor white
Copy-Item -Path $file -Destination $newPath -recurse -Force
}
}
Write-Progresswhich should come in handy. Beneath that: Is there a reason why you have the same copy-code inside the if-block and inside the else-block? Why not just go for an if to ensure that the folder exists and copy beneath all that in a single operation?