0

the below is the code ,in which I am comparing values.

 $b=100
 $c=100,200,300
  foreach ($vvv in $c){

if($vvv -eq $b)
   {
  Write-Host $vvv
}
   Write-Host $vvv "value"
  }

Result the output for the above code is

  100
  100 value
  200 value
  300 value 

but the requirement here is I don't want to use the else loop here ,and I need the value which is not entering to the if loop

 The output I need is 
  200
  300
saraa
  • 17
  • 4
  • 1
    Just: `$c -ne $b`, see: [**When the input is a collection, the operator returns the elements of the collection that match the right-hand value of the expression.**](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comparison_operators?view=powershell-7.1#common-features) – iRon Feb 23 '21 at 11:00
  • My requirement is i need "the variable value which is not executing in if loop " .If i change the operator (equal to or anything ). i just only need the value which is not executing in the if loop ,with out using the Else – saraa Feb 24 '21 at 02:12

1 Answers1

1

The below will give out the expected results, where values in $c array are not in $b array.

$b=100
$c=100,200,300

$Results = $C | Where {$_ -notin $b}

$Results
CraftyB
  • 721
  • 5
  • 11
  • 1
    As commented in the question, there is no reason to use expensive long-winded (cmdlet) iterators as [`Where-Object`](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/where-object) for this, just: `$Results = $c -ne $b` – iRon Feb 23 '21 at 11:07
  • @iRon - If you change `$b=100` to `$b=100,300`, the result will be 100,200,300. I think the OP is trying to understand how to compare one array against another. If they extend the comparing array the example you have given will not work. – CraftyB Feb 23 '21 at 11:45
  • In that case, it is probably a duplicate with [Comparing two arrays & get the values which are not common](https://stackoverflow.com/a/35872835/1701026) – iRon Feb 23 '21 at 12:57