比较两个列表,并使用powershell查找列表一和列表二中的名称 [英] Compare two list and find names that are in list one and not list two using powershell
问题描述
我只是想知道您是否可以帮助我。.我试图比较两个列表(txt文件),并找到列表A中而不是列表B中的字符串,并将其输出到另一个txt文件中。.任何人都知道如何
Just wondering if you can help me out.. I am trying to compare two list(txt file) and find strings that are in list A and not in List B and output it to another txt file.. anybody know how to do it using powershell ?
这是我到目前为止的内容:
Here is what I have so far:
Compare-Object -ReferenceObject $FolderLists -DifferenceObject $AdUserName -passThru
我想查找所有$ FolderLists中的字符串,而不是$ AdUserName中的字符串,并可能将其输出到另一个变量。我遇到的问题是它输出不在两个列表中的字符串。
I would like to find all strings that are in $FolderLists and not $AdUserName and possibly output it to another variable. The issue I am having is that it outputs strings that are not in both lists.
推荐答案
我假设 $ FolderList 和 $ AdUserName 是字符串数组吗?您实际上不需要比较对象来比较数组。就像这样简单:
I assume $FolderList and $AdUserName are arrays of strings? You don't really need Compare-Object to compare arrays. It's as simple as this:
$FolderList | ?{$AdUserName -notcontains $_}
  ;
比较对象用于将对象集合的指定属性与公共属性进行比较。如果您确实愿意,可以可以使用比较对象来做到这一点,例如:
Compare-Object is for comparing the specified properties of collections of objects with common properties. You could do this with Compare-Object if you really want, like this:
Compare-Object $FolderList $AdUserName | ?{$_.SideIndicator -eq '<='} | Select-Object -ExpandProperty InputObject
但是您可以看到,对于此任务而言,这太过分了。
But as you can see, it's overkill for this task.
要将结果输出到另一个变量,只需对其进行分配:
To output the result to another variable, simply assign it:
$AnotherVariable = $FolderList | ?{$AdUserName -notcontains $_}
这篇关于比较两个列表,并使用powershell查找列表一和列表二中的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!