I have a list of file objects, and I want to check whether a given file object appears inside that list. The -Contains
operator is nearly what I'm looking for, but it appears that -Contains
uses a very strict equality test where object references have to be identical. Is there some less strict alternative? I want the $boolean
variable in the code below to return True
the second time as well as the first time.
PS C:\Users\Public\Documents\temp> ls
Directory: C:\Users\Public\Documents\temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 14.08.2017 18.33 5 file1.txt
-a---- 14.08.2017 18.33 5 file2.txt
PS C:\Users\Public\Documents\temp> $files1 = Get-ChildItem .
PS C:\Users\Public\Documents\temp> $files2 = Get-ChildItem .
PS C:\Users\Public\Documents\temp> $file = $files1[1]
PS C:\Users\Public\Documents\temp> $boolean = $files1 -Contains $file
PS C:\Users\Public\Documents\temp> $boolean
True
PS C:\Users\Public\Documents\temp> $boolean = $files2 -Contains $file
PS C:\Users\Public\Documents\temp> $boolean
False
PS C:\Users\Public\Documents\temp>