# Compare content of text file with directory and move the common result

**URL:** <https://forums.powershell.org/t/compare-content-of-text-file-with-directory-and-move-the-common-result/21659>\
**Category:** PowerShell Help\
**Created:** [February 15, 2023, 5:19pm UTC](https://forums.powershell.org/t/compare-content-of-text-file-with-directory-and-move-the-common-result/21659 "2023-02-15T17:19:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![zwymar](https://avatars.discourse-cdn.com/v4/letter/z/b5e925/32.png) [@zwymar](https://forums.powershell.org/u/zwymar)\
**Post date:** [February 15, 2023, 5:19pm UTC](https://forums.powershell.org/t/compare-content-of-text-file-with-directory-and-move-the-common-result/21659/1 "2023-02-15T17:19:40Z")

</div>

I have a textfile which contains the name of files that are missing in a certain destination folder. Therefore I try to compare it to our archivefolder. All the files that are in the list should be copied to a destinationfolder. If you have any suggestion, that would be helpful!

Here is an example:  
#script to compare files and move the right collection

# 

$files = Get-Content -Path d:\acdc.txt  
$searchIn = Get-ChildItem -Path d:\acdc\

foreach ($file in $files)

{  
if ($searchIn -contains $file)  
{  
copy-item -Path D:\a-folder  
}

}

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [February 15, 2023, 5:32pm UTC](https://forums.powershell.org/t/compare-content-of-text-file-with-directory-and-move-the-common-result/21659/2 "2023-02-15T17:32:25Z")

</div>

When you post code, sample data, console output or error messages please format it as code using the _preformatted text_ button ( **\</\>** ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

[How to format code in PowerShell.org](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) \<---- Click 👆🏽 😉

> [@zwymar](#):
>
> Therefore I try to compare it to our archivefolder.

If you want to compare objects you can use

> **[Compare-Object (Microsoft.PowerShell.Utility) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/compare-object?view=powershell-7.3)**
>
> The Compare-Object cmdlet compares two sets of objects. One set of objects is the reference, and the other set of objects is the difference. Compare-Object checks for available methods of comparing a whole object. If it can't find a suitable method,...

Please read the help completely including the examples to learn how to use it. 😉

---

<div class="post-metadata">

**Author:** ![zwymar](https://avatars.discourse-cdn.com/v4/letter/z/b5e925/32.png) [@zwymar](https://forums.powershell.org/u/zwymar)\
**Post date:** [February 16, 2023, 12:52pm UTC](https://forums.powershell.org/t/compare-content-of-text-file-with-directory-and-move-the-common-result/21659/4 "2023-02-16T12:52:10Z")

</div>

ok, I tried to do it with compare…still have a problem.  
In the destination folder only the “base folder” is created, but the files are not copied into it.

```auto
#List with all the files that we need to copy to a target directory
$files = Get-Content -Path "H:\Skripte\SearchListDirectory.txt"

#archive directory with a backup of all files - we filter to get a smaller result - we need only the .xml files
$archiv = Get-ChildItem -path "K:\temp\zwma\20230215" -Exclude *.pdf,*_log.xml -name

#comapare the directory with the content of the file - the result need to be restored
$selection = Compare-Object -ReferenceObject $files -DifferenceObject $archiv -IncludeEqual | Where-Object {$_.SideIndicator -eq "=="} | select inputobject

# Compare-Object -ReferenceObject $files -DifferenceObject $archiv -IncludeEqual | Where-Object {$_.SideIndicator -eq "=="} | gm

#copy files from backup directory to target directory.

ForEach ($select in $selection) {
            
  Copy-Item -path "K:\temp\zwma\20230215\$($selct.basename)" -destination "K:\temp\zwma\NachSendenOIZ\$select" -Force
}

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [February 16, 2023, 3:52pm UTC](https://forums.powershell.org/t/compare-content-of-text-file-with-directory-and-move-the-common-result/21659/5 "2023-02-16T15:52:10Z")

</div>

> [@zwymar](#):
>
> ```auto
> $selection = Compare-Object -ReferenceObject $files -DifferenceObject $archiv -IncludeEqual | Where-Object {$_.SideIndicator -eq "=="} | select inputobject
> 
> ```

If you use the parameter `-ExcludeDifferent` you don’t need the `Where-Object`. 😉

> [@zwymar](#):
>
> ```auto
> Copy-Item -path "K:\temp\zwma\20230215\$($selct.basename)" -destination "K:\temp\zwma\NachSendenOIZ\$select" -Force
> 
> ```

`Copy-Item` by default expects a folder path as destination. And If you don’t want to rename the file while copying it you don’t need a file name.

To debug your code you should use a few test files and output the variables you’re using to check if it’s what you think it is.

> [@zwymar](#):
>
> `-path "K:\temp\zwma\20230215\$($selct.basename)"`

The property `BaseName` is the file name _ **without** _ its extension - this will very likely not work. 😉

> [@zwymar](#):
>
> `"K:\temp\zwma\NachSendenOIZ\$select"`

If `$Select` is an object with properties this might not be what you expect. 😉

---

<div class="post-metadata">

**Author:** ![zwymar](https://avatars.discourse-cdn.com/v4/letter/z/b5e925/32.png) [@zwymar](https://forums.powershell.org/u/zwymar)\
**Post date:** [February 17, 2023, 7:19am UTC](https://forums.powershell.org/t/compare-content-of-text-file-with-directory-and-move-the-common-result/21659/6 "2023-02-17T07:19:38Z")

</div>

Thanks for the input…I changed a few things/name and got still no results, but the errormessage is now helpful.  
PS tries to copy the content of the list instead of the archive files. I switched the refereceObject and DifferenceObject but it didn’t help. How to I tell PS to tkae to files from $arichive?

```auto
#List with all the files that we need to copy to a target directory
$files = Get-Content -Path "H:\Skripte\SearchListDirectory.txt"

#archive directory with a backup of all files - we filter to get a smaller result - we need only the .xml files
$archiv = Get-ChildItem -path "K:\temp\zwma\20230215" -Exclude *.pdf,*_log.xml -name

#target directory
$destination = "K:\temp\zwma\restore\"

#$restoreFiles = Compare-Object -ReferenceObject $files -DifferenceObject $archiv -ExcludeDifferent -IncludeEqual| select inputobject
$restoreFiles = Compare-Object -ReferenceObject $archiv -DifferenceObject $files -ExcludeDifferent -IncludeEqual| select inputobject

#copy files from backup directory to target directory.

ForEach ($restoreFile in $restoreFiles) {
        
      
       # copy files to destination folder
       Copy-Item $restoreFile -Destination $destination
              
       
}

```

Here is the error message -it’s in German - but you can clearly see that it tries to access H:\drive which is the source of the list. - Thanks for some additional input;-)  
PS H:\> H:\Skripte\compareCopyNew.ps1  
Copy-Item : Der Pfad “H:@{InputObject=3010\_007317562\_189.xml}” kann nicht gefunden werden, da er nicht vorhanden ist.  
In H:\Skripte\compareCopyNew.ps1:27 Zeichen:8

- 

```
   Copy-Item $restoreFile -Destination $destination

```

- 

```
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

```

  - CategoryInfo : ObjectNotFound: (H:@{InputObject=3010\_007317562\_189.xml}:String) [Copy-Item], ItemNotFoundException
  - FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand

---

<div class="post-metadata">

**Author:** ![zwymar](https://avatars.discourse-cdn.com/v4/letter/z/b5e925/32.png) [@zwymar](https://forums.powershell.org/u/zwymar)\
**Post date:** [February 17, 2023, 11:31am UTC](https://forums.powershell.org/t/compare-content-of-text-file-with-directory-and-move-the-common-result/21659/7 "2023-02-17T11:31:51Z")

</div>

Hi Olaf,  
found a soluten…

```auto
#Source Directory non-filtered
$PfadFROM = "K:\temp\zwma\20230215\"

#target directory
$destination = "K:\temp\zwma\NachSendenOIZ\"

#List with all the files that we need to copy to a target directory
$files = Get-Content -Path "K:\temp\zwma\SearchListDirectory.txt"

#### DEBUG - Ausgabe aller Filenamen von der Liste ####
#Write-host "Liste: "
#foreach ($filename in $files) {
    #Write-host $filename
#}

#archive directory with a backup of all files - we filter to get a smaller result - we need only the .xml files
$archiv = Get-ChildItem -path "K:\temp\zwma\20230215\" -Exclude *.pdf,*_log.xml -name
#Write-Host $archiv

#### DEBUG - Ausgeben aller Filenamen im Ordner (gefilteret)
#Write-host "Ordner Files: "
#foreach ($archivdatei in $archiv) {
    #Write-host $archivdatei
#}

##Unterschiede herausssuchen (und nur Name ausgeben)
#Write-Output "comparison"
$restoreFiles = Compare-Object -ReferenceObject $files -DifferenceObject $archiv -ExcludeDifferent -IncludeEqual | Select Object -ExpandProperty InputObject

#Für jeden treffer
ForEach ($restoreFile in $restoreFiles) {
    ##### Debug - Alle treffer ausgeben ####
    #Write-Host $restoreFile

    # Pfad FROM zusammenstellen
    $FullFileNameWithPathFROM = $PfadFROM + $restoreFile
    #Write-Host "FROM" $FullFileNameWithPathFROM
    # Pfad TO zusammenstellen
    $FullFileNameWithPathTO = $destination + $restoreFile
    #Write-Host "TO" $FullFileNameWithPathTO

    Copy-Item $FullFileNameWithPathFROM -Destination $FullFileNameWithPathTO      
}

```

---

<div class="post-metadata">

**Author:** ![zwymar](https://avatars.discourse-cdn.com/v4/letter/z/b5e925/32.png) [@zwymar](https://forums.powershell.org/u/zwymar)\
**Post date:** [February 17, 2023, 11:32am UTC](https://forums.powershell.org/t/compare-content-of-text-file-with-directory-and-move-the-common-result/21659/8 "2023-02-17T11:32:49Z")

</div>

Thank you very much! Have a nice weekend! Cheers 🥂  
🥂

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [February 17, 2023, 11:40am UTC](https://forums.powershell.org/t/compare-content-of-text-file-with-directory-and-move-the-common-result/21659/9 "2023-02-17T11:40:50Z")

</div>

Wow … you’re overcomplicating this a lot. 😉

You may use `-passThru` to actually get the file system objects.

> **[Compare-Object (Microsoft.PowerShell.Utility) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/compare-object?view=powershell-7.3#-passthru)**
>
> The Compare-Object cmdlet compares two sets of objects. One set of objects is the reference, and the other set of objects is the difference. Compare-Object checks for available methods of comparing a whole object. If it can't find a suitable method,...

```auto
$files = Get-Content -Path "H:\Skripte\SearchListDirectory.txt"
$archiv = Get-ChildItem -path "K:\temp\zwma\20230215" -Exclude *.pdf,*_log.xml -name
$destination = "K:\temp\zwma\restore\"

$restoreFiles = 
    Compare-Object -ReferenceObject $archiv -DifferenceObject $files -ExcludeDifferent -IncludeEqual -PassThru

ForEach ($restoreFile in $restoreFiles) {
    # if you have code not doing what you expect you start with outputting the variables you use.
    $restoreFile.FullName
    Copy-Item $restoreFile.FullName -Destination $destination
}

```

> [@zwymar](#):
>
> -it’s in German

No problem — me too. 😉 🇩🇪

---

<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:15pm UTC](https://forums.powershell.org/t/compare-content-of-text-file-with-directory-and-move-the-common-result/21659/10 "2024-05-16T20:15:07Z")

</div>


