Powershell Only Add to Array if it doesn't exist

后端 未结 2 1462
别那么骄傲
别那么骄傲 2021-01-17 11:49

In PowerShell v2, I\'m trying to add only unique values to an array. I\'ve tried using an if statement that says, roughly, If (-not $Array -contains \'SomeValue\'), then ad

2条回答
  •  一整个雨季
    2021-01-17 12:24

    To fix your code, try -notcontains or at least WRAP your contains-test in parantheses. Atm. your test reads:

    If "NOT array"(if array doens't exist) contains word.

    This makes no sense. What you want is:

    If array does not contain word..

    That's written like this:

    If (-not ($IncorrectArray -contains $Word))
    

    -notcontains is even better, as @dugas suggested.

提交回复
热议问题