PowerShell v3 Ipconfig and Select String
The purpose of this page is control the output of Ipconfig with PowerShell’s Select-String.
PowerShell v3 Ipconfig
- Introduction to PowerShell and Ipconfig
- Example 1: To List Just IP Addresses
- Example 2: To Display the Default Gateway
- Troubleshooting Ipconfig
♣
Introduction to PowerShell and Ipconfig
Ipconfig has been built-in to each Microsoft operating systems from Windows 3.11 onwards. My primary use of this utility is to display a computer’s IP or MAC address. Other common use of Ipconfig is to release and renew IP address leases for DHCP clients.
Traditionally, Ipconfig has been used in a DOS box. One of my ways of weaning people away from CMD.exe and into PowerShell ISE, is to persuade them to run native utilities, such as Ipconfig, in PowerShell instead of cmd.
The purpose of this page is to give extra reasons for using PowerShell by adding useful supplementary commands such as Select-String, or its new alias ‘sls’.
Example 1: To List Just IP Addresses
Let us take stock: we are using the built-in operating system command Ipconfig and then piping (|) the output into Select-String, where we filter the data for ‘IP’ information.
Ipconfig | Select-String IP
Result:
Windows IP Configuration
Link-local IPv6 Address: fe80::c45b:a402:b918:8454%10
IPv4 Address: 192.168.1.47
IPv6 Address: 2001:0:5ef5:79fb:1c91:379:3f57:fed3
Link-local IPv6 Address: fe80::1c91:379:3f57:fed3%13
Note 1: For comparison you could try plain:
Ipconfig
Example 2: To Display the Default Gateway
The above example almost begs you to select other information, for instance, instead of ‘IP’ you could try ‘IPv4’, ‘Gateway’, or even DNS. Also observe below how I substitute the alias ‘sls’ for Select-String.
Clear-Host
Ipconfig | sls Gate
Result:
Default Gateway . . . . . . . . . : 192.168.1.254
See more on PowerShell’s Select-String »
Guy Recommends: A Free Trial of the Network Performance Monitor (NPM) v11.5
SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network. This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.
What I like best is the way NPM suggests solutions to network problems. Its also has the ability to monitor the health of individual VMware virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.
Download a free trial of Solarwinds’ Network Performance Monitor
Research More Ipconfig Switches
Get-Help does not work with Ipconfig, however you can use its native help command /? thus:
Clear-Host
Ipconfig /?
Amongst the parameters listed /release and /renew the most commonly used, and useful commands that are often forgotten are: Ipconfig /flushdns and /displaydns.
Troubleshooting Why Ipconfig is Not Working
Here are ideas for using PowerShell to overcome common problems with Ipconfig.
Remember the Switch: Ipconfig /All
Problem: Why can’t you see the MAC address?
Solution: PowerShell with Ipconfig’s /All parameter.
# PowerShell MAC Address script
Clear-Host
Ipconfig | sls Physical
# Compare with /all
Ipconfig /all | sls Physical
Physical Address. . . . . . . . . : 20-CF-30-3A-B5-72
Physical Address is the formal name for MAC address.
See top 10 administrative commands to run in PowerShell »
Troubleshooting Ipconfig
Problem: ‘Ipconfig’ is not recognized as an internal or external command, operable program or batch file.
Solution: Check the path to Ipconfig:
Clear-Host
($env:Path).Split(";")
Result: You should see: C:\Windows\system32; if not then append this folder to the path thus:
Clear-Host
$env:Path = $env:Path +";$env:windir\system32;
Check that the executable exists: just in case someone has deleted ipconfig try this:
Clear-Host
Test-Path -path "$env:windir\system32\ipconfig.exe"
True
Build a Function Get-IPConfig
Another strategy is to build your own PowerShell function based on ideas from this page. For example we could build a function called -MAC which would display the Physical Address, for example: 20-CF-30-3A-B3-73.
See more about creating the Get-IPConfig function »
Summary of PowerShell Ipconfig with Select-String
Ipconfig was once used exclusively in a DOS box. One of my ways of weaning people away from CMD.exe and into PowerShell ISE, is to persuade them to run native utilities, such as Ipconfig, in PowerShell instead of cmd. The killer reason for switching is combining Ipconfig with Select-String.
If you like this page then please share it with your friends
See more Microsoft PowerShell tutorials
• PowerShell Tutorials • Methods • Cmdlets • PS Snapin • Profile.ps1 • Exchange 2007
• Command & Expression Mode • PowerShell pipeline (|) • PowerShell ‘where‘ • PowerShell ‘Sort’
• Windows PowerShell Modules • Import-Module • PowerShell Module Directory
If you see an error of any kind, do let me know. Please report any factual mistakes, grammatical errors or broken links.