windows 使用powershell比较两个列表并查找列表一中而不是列表二中的名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19012457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compare two list and find names that are in list one and not list two using powershell
提问by tyson619
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 ?
只是想知道您是否可以帮助我.. 我正在尝试比较两个列表(txt 文件)并找到列表 A 中而不是列表 B 中的字符串并将其输出到另一个 txt 文件中.. 任何人都知道如何使用电源外壳 ?
Here is what I have so far:
这是我到目前为止所拥有的:
Compare-Object -ReferenceObject $FolderLists -DifferenceObject $AdUserName -passThru
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.
我想找到 $FolderLists 而不是 $AdUserName 中的所有字符串,并可能将其输出到另一个变量。我遇到的问题是它输出不在两个列表中的字符串。
回答by Adi Inbar
I assume $FolderListand $AdUserNameare arrays of strings? You don't really need Compare-Objectto compare arrays. It's as simple as this:
我假设$FolderList和$AdUserName是字符串数组?您实际上并不需要Compare-Object来比较数组。就这么简单:
$FolderList | ?{$AdUserName -notcontains $_}
Compare-Objectis for comparing the specified properties of collections of objects with common properties. You coulddo this with Compare-Objectif you really want, like this:
Compare-Object用于将对象集合的指定属性与公共属性进行比较。如果你真的想要,你可以用Compare-Object来做到这一点,就像这样:
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 $_}