0

Get all the Network Interfaces

$nics = Get-AzNetworkInterface | ?{ $_.VirtualMachine -NE $null}

I am stuck on ?{ $_.VirtualMachine -NE $null} can anyone help?

  • 2
    As answered by @Abdul, the question mark (`?`) is a shortcut for the [`Where-Object`](https://docs.microsoft.com/powershell/module/microsoft.powershell.core/where-object) and what follows between the curly brackets is a comparison expression as answered by @RithwikBojja-MT. The important issue here to mention is that the statement is not according to the best practice as `$Null` should be at the left hand side of the comparison operator (in this case `-NE`), see: [**Checking for `$Null`**](https://docs.microsoft.com/powershell/scripting/learn/deep-dives/everything-about-null#checking-for-null) – iRon Jul 13 '22 at 05:59
  • Please [format your post properly](https://stackoverflow.com/help/formatting). – mklement0 Jul 13 '22 at 13:40

2 Answers2

2

? is an alias for Where-Object Cmdlet. That means

$nics = Get-AzNetworkInterface | ?{ $_.VirtualMachine -NE $null}

is equivalent to

$nics = Get-AzNetworkInterface | Where-Object { $_.VirtualMachine -NE $null}

Here Where-Object selects objects from a collection based on their property values and $_ is a variable to refer the current item in the collection.

Abdul Niyas P M
  • 18,035
  • 2
  • 25
  • 46
0

?{ $_.VirtualMachine -NE $null}

NE is Comparision Operator which means Not Equals and $_ means it is using the present item. So, it means finding those Virtual Machines which are not equal to null in the collection.

RithwikBojja
  • 5,069
  • 2
  • 3
  • 7