Hello Guys,

I am trying to use Copy-Item to copy a directory. I want to check if the file does not exist and then copy the missing files.

I have written this script so far but it seems to only fix missing directories not files. Any idea what might be wrong?

$Source1 = ($env:userprofile + "\Favorites\")
$source1list = Get-ChildItem $Source1
$destination1 = ("\\Ancurfs2\andata2\" + $env:username + "\Profile\Favorites\")

foreach ($file in $source1list)
{
   $result = test-path -path "$destination1\*" -include $file.Name
   
   if ($result -like "False")
   {
        Copy-Item "$source1\$file" -Destination "$destination1"  -Recurse

   } 
}

4 Spice ups

Hm have to look at the rest, but just off the bat, use

if ($result -eq $false) {blah blah}

not ‘-like ‘false’’

This seems to work just fine for me:
Also Maybe try this :

$destination1 = "\\Ancurfs2\andata2\$env:username\Profile\Favorites\"
$Source1 = ($env:userprofile + "\Favorites\")
$source1list = Get-ChildItem $Source1
$destination1 = "c:\test" # Try to change this to the above mentioned

foreach ($file in $source1list)
{
   $result = test-path -path "$destination1\*" -include $file.Name
   
   if ($result -like "False")
   {
        Copy-Item "$source1\$file" -Destination "$destination1"  -Recurse

   } 
}

Sounds like robocopy would be a better fit for your need.

2 Spice ups

Hello,

Initially it seems to work but if you delete a file inside on of the folders and re-run the script. The file is not copied back. So the Source and Destination are then out of sync.

Ohhh, you want to purge? So if a file was deleted form the source, you want it deleted on the destination as well?

Because it works the other way around, I deleted several files in the destination and re-run it and it put the files I deleted back in the folder.

I have considered robocopy but I would like to stick to powershell to be able to use Powershell variables.

No. If the file on the destination was deleted then the script does not re-copy it from the source.

Understand?

It does in my test.

First of all you never use -Like on a Boolean value. Take a look at this see if it fits the bill.

$Source1 = Join-Path -Path $env:userprofile -ChildPath "Favorites"
$source1list = Get-ChildItem $Source1
$destination1 = "\\Ancurfs2\andata2\$($env:username)\Profile\Favorites\"
# create directory if needed
if (-not (Test-Path -Path $destination1))
{
    md -Path $destination1 -Force | Out-Null
}
$cnt = 0
foreach ($file in $source1list)
{
   if (-not (Test-Path -Path (Join-Path -Path $destination1 -ChildPath $($file.Name))))
   {
        Copy-Item $($file.FullName) -Destination $destination1
        $cnt++
   } 
}
Write-Verbose "Copied $cnt files to $destination1"

1 Spice up

I think it would help everyone better if you explain what you need the script for because the quoted comment is confusing.

Are you trying to create backups and do restores? Are you setting up a template directory and copying it to all users?

True. but - NEVER check a boolean for equality to $true/$false, just

If ($result)  { blah blah if true blah blah}

or

(if (-Not $result) {'stuff that means result if false'}
3 Spice ups