This example will get the current IP address(s) of the user`s machine.
PS C:\Users\Intel i5> Get-NetIPAddress | Sort-Object -Property InterfaceIndex | where-object -FilterScript {$_.SuffixOrigin -eq "Dhcp"} IPAddress : 192.168.1.114 InterfaceIndex : 10 InterfaceAlias : Ethernet AddressFamily : IPv4 Type : Unicast PrefixLength : 24 PrefixOrigin : Dhcp SuffixOrigin : Dhcp AddressState : Preferred ValidLifetime : 23:35:27 PreferredLifetime : 23:35:27 SkipAsSource : False PolicyStore : ActiveStore |
This example using Format-table formats the data in a nice readable format.
PS C:\Users\Intel i5> Get-NetIPAddress | Sort-Object -Property InterfaceIndex | where-object -FilterScript {$_.SuffixOrigin -eq “Dhcp”} | format-table
ifIndex IPAddress PrefixLength PrefixOrigin SuffixOrigin AddressState PolicyStore
——- ——— ———— ———— ———— ———— ———–
10 192.168.1.114 24 Dhcp Dhcp Preferred ActiveStore
Powershell is actually very flexible in terms of formatting the output of certain cmdlets.
This example will convert all IP address information output by the Get-NetIPAddress cmdlet into Json format.
PS C:\Users\Intel i5> Get-NetIPAddress | Sort-Object -Property InterfaceIndex | ConvertTo-Json |
This works very well.
You may also convert the output into an HTML table just by piping the output into the ConvertTo-Html cmdlet. This is amazing.
PS C:\Users\Intel i5> Get-NetIPAddress | Sort-Object -Property InterfaceIndex | where-object -FilterScript {$_.SuffixOrigin -eq "Dhcp"} | ConvertTo-Html |
Or you may even convert the data to CSV format to import into a spreadsheet in Microsoft Office.
PS C:\Users\Intel i5> Get-NetIPAddress | Sort-Object -Property InterfaceIndex | where-object -FilterScript {$_.SuffixOrigin -eq "Dhcp"} | ConvertTo-Csv |
Very useful Powershell cmdlets.
This example below is how to further filter the output when creating a table and only show the columns that you actually wish to see.
PS C:\Users\Intel i5> Get-NetIPAddress | Sort-Object -Property InterfaceIndex | where-object -FilterScript {$_.SuffixOrigin -eq "Dhcp"} | Format-Table -Property IPAddress,InterfaceAlias IPAddress InterfaceAlias --------- -------------- 192.168.1.114 Ethernet |
A very useful text filtering trick.