Powershell sometimes can be challenging. One of the more confusing things is collecting data and having them in proper formatted tables / objects to process them further.
The below script is a sheer example on how you can accomplish this while creating a table, adding the needed columns and then filling the table with rows – in this specific example while reading network adapters and filling in IP information – if available – partly multiple rows (one row per IP and adapter) and partly single rows for adapters with not IPs.
Custom tables or custom PowerShell objects example with foreach loops to fill them up and combine values from various commands in to single tables for further processing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | $tbl = New-Object System.Data.DataTable "NetworkAdaptersAndIPs" $col1 = New-Object System.Data.DataColumn Name $col2 = New-Object System.Data.DataColumn Interface $col3 = New-Object System.Data.DataColumn Status $col4 = New-Object System.Data.DataColumn MACAddress $col5 = New-Object System.Data.DataColumn IPAddress $col6 = New-Object System.Data.DataColumn LinkSpeed $tbl.Columns.Add($col1) $tbl.Columns.Add($col2) $tbl.Columns.Add($col3) $tbl.Columns.Add($col4) $tbl.Columns.Add($col5) $tbl.Columns.Add($col6) foreach ($Adapter in Get-NetAdapter) { If ($Adapter.Status -eq 'Up') { foreach ($IP in Get-NetIPAddress -InterfaceIndex $Adapter.InterfaceIndex) { $row = $tbl.NewRow() $row.Name = $Adapter.Name $row.Interface = $Adapter.InterfaceDescription $row.Status = $Adapter.Status $row.MACAddress = $Adapter.MacAddress $row.IPAddress = $IP.IPAddress $row.LinkSpeed = $Adapter.LinkSpeed $tbl.Rows.Add($row) } } else { $row = $tbl.NewRow() $row.Name = $Adapter.Name $row.Interface = $Adapter.InterfaceDescription $row.Status = $Adapter.Status $row.MACAddress = $Adapter.MacAddress $row.LinkSpeed = $Adapter.LinkSpeed $tbl.Rows.Add($row) } } $tbl | fl |