-1

I'm trying to combine System.IO.FileInfo objects (from distinct Get-ChildItem calls) together. I've found working solutions (i.e. using PowerShell array) from this question: Combine the results of two distinct Get-ChildItem calls into single variable to do the same processing on them

$target = "C:\example"

$Files = @( Get-ChildItem -LiteralPath $target -Force -Attributes !D )
$Files += @( Get-ChildItem -LiteralPath $target -Force -Attributes !D ) # for demo. & simplicity, I'm using the same path here

$Files | Write-Host # here the same entries are duplicated

However, the same entries from the System.IO.FileInfo objects are duplicated in the resulting object. I'm wondering if there is an elegant way to combine the objects while removing the duplicates?

PS: Files are "duplicated" if they have the same ".FullName".

SHpj123
  • 1
  • 1
  • Define "duplicate". Your comment suggests each call to `Get-ChildItem` will be passed a different `-LiteralPath`, so what constitutes a duplicate file? Same name, relative path, contents/hash, or something else? – Lance U. Matthews Oct 11 '22 at 07:14
  • Hi, I've updated the question (Files are "duplicated" if they have the same ".FullName". ) – SHpj123 Oct 11 '22 at 07:23
  • Is one path passed to `Get-ChildItem` an ancestor of the other? Even then, without `-Recurse` the output file objects can never have the same `FullName`. Please show an actual sample of what you want to run because your `# for demo. & simplicity, I'm using the same path` comment isn't making things more clear. As written, if you don't want duplicate `FileInfo`s then simply _don't call `Get-ChildItem` twice_. – Lance U. Matthews Oct 11 '22 at 16:04
  • thx for the reply, filimonic's answer already solves the problem – SHpj123 Oct 11 '22 at 16:45

1 Answers1

1
$Files = @($Files | Sort-Object -Property FullName -Unique)
filimonic
  • 3,988
  • 2
  • 19
  • 26