# Compare, Delete and copy

**URL:** <https://forums.powershell.org/t/compare-delete-and-copy/4059>\
**Category:** PowerShell Help\
**Created:** [April 17, 2015, 1:37am UTC](https://forums.powershell.org/t/compare-delete-and-copy/4059 "2015-04-17T01:37:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikhael-kaendera](https://avatars.discourse-cdn.com/v4/letter/m/c5a1d2/32.png) [@mikhael-kaendera](https://forums.powershell.org/u/mikhael-kaendera)\
**Post date:** [April 17, 2015, 1:37am UTC](https://forums.powershell.org/t/compare-delete-and-copy/4059/1 "2015-04-17T01:37:31Z")

</div>

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

```

}

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [April 17, 2015, 3:10am UTC](https://forums.powershell.org/t/compare-delete-and-copy/4059/2 "2015-04-17T03:10:28Z")

</div>

```
$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}
   } 
}
```

---

<div class="post-metadata">

**Author:** ![mikhael-kaendera](https://avatars.discourse-cdn.com/v4/letter/m/c5a1d2/32.png) [@mikhael-kaendera](https://forums.powershell.org/u/mikhael-kaendera)\
**Post date:** [April 17, 2015, 5:12am UTC](https://forums.powershell.org/t/compare-delete-and-copy/4059/3 "2015-04-17T05:12:10Z")

</div>

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}  
}  
}

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [April 17, 2015, 5:16am UTC](https://forums.powershell.org/t/compare-delete-and-copy/4059/4 "2015-04-17T05:16:12Z")

</div>

There is an extra double quote before Copy-Item

---

<div class="post-metadata">

**Author:** ![mikhael-kaendera](https://avatars.discourse-cdn.com/v4/letter/m/c5a1d2/32.png) [@mikhael-kaendera](https://forums.powershell.org/u/mikhael-kaendera)\
**Post date:** [April 17, 2015, 5:18am UTC](https://forums.powershell.org/t/compare-delete-and-copy/4059/5 "2015-04-17T05:18:00Z")

</div>

Thank you

---

<div class="post-metadata">

**Author:** ![mikhael-kaendera](https://avatars.discourse-cdn.com/v4/letter/m/c5a1d2/32.png) [@mikhael-kaendera](https://forums.powershell.org/u/mikhael-kaendera)\
**Post date:** [April 17, 2015, 5:19am UTC](https://forums.powershell.org/t/compare-delete-and-copy/4059/6 "2015-04-17T05:19:35Z")

</div>

Rob ,

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

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [April 17, 2015, 5:32am UTC](https://forums.powershell.org/t/compare-delete-and-copy/4059/7 "2015-04-17T05:32:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mikhael-kaendera](https://avatars.discourse-cdn.com/v4/letter/m/c5a1d2/32.png) [@mikhael-kaendera](https://forums.powershell.org/u/mikhael-kaendera)\
**Post date:** [April 17, 2015, 5:44am UTC](https://forums.powershell.org/t/compare-delete-and-copy/4059/8 "2015-04-17T05:44:30Z")

</div>

Thank you very much I will look it up.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:46pm UTC](https://forums.powershell.org/t/compare-delete-and-copy/4059/9 "2024-05-16T20:46:12Z")

</div>


