问题描述
只是想知道您是否可以帮助我.. 我正在尝试比较两个列表(txt 文件)并找到列表 A 中而不是列表 B 中的字符串并将其输出到另一个 txt 文件中.. 任何人都知道如何用 powershell 做吗?
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 ?
这是我目前所拥有的:
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 是字符串数组?您真的不需要 Compare-Object 来比较数组.就这么简单:
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 用于将对象集合的指定属性与公共属性进行比较.如果您真的想要,可以使用 Compare-Object 执行此操作,如下所示:
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查找列表一中的名称而不是列表二的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!