I am attempting to use Compare-Object
to find elements that are in one array ($fileArray
) but not in another ($dictionaryArray
). For some reason, Compare-Object
is only comparing the last two elements in fileArray. Here is the code I am trying to run.
$dictionary = Get-Content "C:\Users\Joe\Documents\PowerShell Scripts\Work\PCI Numbers\dictionary.txt"
$file = Get-Content "C:\Users\Joe\Documents\PowerShell Scripts\Work\PCI Numbers\file.txt"
$dictionaryArray = @()
$fileArray = @()
$dictionaryLength = $dictionary | Measure-Object -Line
$fileLength = $file | Measure-Object -Line
$i = 0
while($i -lt $dictionaryLength.Lines){
$name = $dictionary | Select -Index $i
$i++
while($dictionary[$i] -ne " " -and $i -lt $dictionaryLength.Lines){
$dictionaryObject = New-Object -TypeName PSObject
$dictionaryObject | Add-Member -Name 'PC' -MemberType Noteproperty -Value $name
$dictionaryObject | Add-Member -Name 'File' -MemberType Noteproperty -Value $dictionary[$i]
$dictionaryArray += $dictionaryObject
$i++
}
$i++
}
$i = 0
while($i -lt $fileLength.Lines){
$name = $file | Select -Index $i
$i++
while($file[$i] -ne " " -and $i -lt $fileLength.Lines){
$fileObject = New-Object -TypeName PSObject
$fileObject | Add-Member -Name 'PC' -MemberType Noteproperty -Value $name
$fileObject | Add-Member -Name 'File' -MemberType Noteproperty -Value $file[$i]
$fileArray += $fileObject
$i++
}
$i++
}
$i = 0
Compare-Object -ReferenceObject $fileArray -DifferenceObject $dictionaryArray |
Where-Object {$_.SideIndicator -eq '<='} |
ForEach-Object {Write-Output $_.InputObject}
When I simply run Compare-Object -ReferenceObject $fileArray
-DifferenceObject $dictionaryArray
, I get the output
InputObject SideIndicator
@{PC=Joe; File=nopethere} <=
@{PC=Joe; File=hi!!!@} <=
There are more items than this in the fileArray. When I run $fileArray
, I get
PC File
Eric I like to
Eric there
Joe code because
Joe hello
Joe but why?
Joe *no\thank/ you :c
Joe nopethere
Joe hi!!!@
Why is my code not comparing each one of these objects?
EDIT
Here is dictionary.txt
Eric
because
hello there
Joe
but why?
*no\thank/ you :c
nope
Here is file.txt
Eric
I like to
there
Joe
code because
hello
but why?
*no\thank/ you :c
nopethere
hi!!!@
I use my loop to create objects. The first line of each paragraph is the "PC" for each subsequent object. Then, each line is the "File" of each object. I want to check which objects are in fileArray and not in dictionaryArray. I would expect the output to be
PC File
Eric I like to
Joe code because
Joe hello
Joe nopethere
Joe hi!!!@
Any help would be appreciated!