Author Posts

April 17, 2015 at 7:07 am

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
}

April 17, 2015 at 8:40 am

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

April 17, 2015 at 10:42 am

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

April 17, 2015 at 10:46 am

There is an extra double quote before Copy-Item

April 17, 2015 at 10:49 am

Rob ,

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

April 17, 2015 at 11:02 am

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.

April 17, 2015 at 11:14 am

Thank you very much I will look it up.