Compare, Delete and copy

Hi there,

I am trying to put tohether a script that compares two folders.

  • If an item is found in the $fso folder but not in $fsoBU delele the item.
    -If an item is found in $fsoBU folder but not in the $fso folder copy the item.

$fso = Get-ChildItem -Recurse -path “\vfs04\Users$\kaendm\Desktop\Test”

$fsoBU = Get-ChildItem -Recurse -path “\vfs04\Users$\kaendm\Desktop\Old Files”

Compare the two folders

$matches = compare-Object -ReferenceObject $fso -DifferenceObject $fsoBU -IncludeEqual

foreach ($fso in $matches)
{
# If an item is $fso folder and not in $fsoBU folder delete the item.

    { if  ($fso -NotMatch) 
       Remove-Item \\vfs04\Users$\kaendm\Desktop\test\*.ini

  # if the an item is in fsoBU folder, but not in $fso folder copy the item to $fso. Copy the missing files.

else	
	Copy-Item fsoBU $fso -recurse -force

}

$files = Get-ChildItem -Path C:\Users\rsimmers\Desktop -File | Select Name, FullName
$files2 = Get-ChildItem -Path C:\Users\rsimmers\Desktop\Archive -File | Select Name, FullName

Compare-Object -ReferenceObject $files -DifferenceObject $files2 -Property Name -PassThru |
foreach {
   $fullPath = $_.FullName
   switch ($_.SideIndicator) {
       "=>"{Remove-Item -Path $fullPath -WhatIf}
       "<="{Copy-Item -Path $fullPath -Destination "C:\Temp" -WhatIf}
   } 
}

Rob,

Were you able to get the script to run? When I run it I am get the attached error messages. See attached.

$files = Get-ChildItem -Path “\vfs04\Users$\kaendm\Desktop\Test” -File | Select Name, FullName
$files2 = Get-ChildItem -Path “\vfs04\Users$\kaendm\Desktop\Old Files” -File | Select Name, FullName

Compare-Object -ReferenceObject $files -DifferenceObject $files2 -Property Name -PassThru |
foreach {
$fullPath = $.FullName
switch ($
.SideIndicator) {
“=>”{Remove-Item -Path $fullPath -WhatIf}
“<=”"{Copy-Item -Path $fullPath -Destination “C:\Temp” -WhatIf}
}
}

There is an extra double quote before Copy-Item

Thank you

Rob ,

I know there are so many powershell books on the market. What book would you recommend for beginners.

Personally, I have not read it, but Don Jones’ book for Learn Powershell in a Month of Lunches. It’s broken up into chunks to learn at a time, so it should be easy to pick up where you left off andcan learn at your own pace.

Thank you very much I will look it up.