Looping through each NoteProperty in a custom object

Try:

ForEach ($noteProperty in $NetworkInfo.PSObject.Properties)
{
   Write-Host $noteProperty.Name
   Write-Host $noteProperty.Value
}

You can use Get-Member as mentioned by Mike; an alternative that keeps the order is to use the PSObject property that is present on all objects:

#View details for all properties on a specific object
$NetworkInfo.PSObject.Properties

#similar code to Mike's, working with name and value:
$NetworkInfo.PSObject.Properties | foreach-object {
    $name = $_.Name 
    $value = $_.value
    "$name = $value"
}

Short (even Faster) version:

$NetworkInfo.psobject.properties.foreach({"$($_.Name) = $($_.Value)"})

Output:

IPAddress = 10.10.8.11
SubnetMask = 255.255.255.0
Gateway = 10.10.8.254
Network = 10.10.8.0
DNS1 = 10.10.10.200
DNS2 = 10.10.10.201

Or another one:

$NetworkInfo.foreach({$_})

Will give you:

IPAddress  : 10.10.8.11
SubnetMask : 255.255.255.0
Gateway    : 10.10.8.254
Network    : 10.10.8.0
DNS1       : 10.10.10.200
DNS2       : 10.10.10.201

You can also select 1 property like:

$NetworkInfo.DNS1

Which will give you:

10.10.10.200

Or select all DNS entries:

$NetworkInfo.foreach({$_}) | Select-Object  DNS*

Result:

DNS1         DNS2
----         ----
10.10.10.200 10.10.10.201

Or if you wish:

$NetworkInfo.psobject.properties.where({$_.Name -like 'DNS*' }).Value

Will give:

10.10.10.200
10.10.10.201

Combining foreach and where:

$NetworkInfo.psobject.properties.where({$_.Name -like 'DNS*' }).foreach({
"$($_.Name) = $($_.Value)"})

Wil give:

DNS1 = 10.10.10.200
DNS2 = 10.10.10.201

Transform it into a 2 dimensional array:

$NetworkInfo.psobject.properties.where({$_.Name -like 'DNS*' }).foreach({
,(@([string]$_.Name, [string]$_.Value))})

Do NOT forget the "," in "{," Output:

DNS1
10.10.10.200
DNS2
10.10.10.201

Which is two dimensional

$NetworkInfo.psobject.properties.where({$_.Name -like 'DNS*' }).foreach({
,(@([string]$_.Name, [string]$_.Value)) })[0]
DNS1
10.10.10.200

And the following:

$NetworkInfo.psobject.properties.where({$_.Name -like 'DNS*' }).foreach({
,(@([string]$_.Name, [string]$_.Value)) })[0][0]
DNS1

And...

$NetworkInfo.psobject.properties.where({$_.Name -like 'DNS*' }).foreach({
,(@([string]$_.Name, [string]$_.Value)) })[0][1]
10.10.10.200

This should get you what you need:

$NetworkInfo | get-member -type NoteProperty | foreach-object {
  $name=$_.Name ; 
  $value=$NetworkInfo."$($_.Name)"
  write-host "$name = $value"
}

Tags:

Powershell