How to Display MAC or IP Address with Get-IPConfig
I designed this cmdlet style function to display IPv4, IPv6 or MAC addresses. At the heart of this example is the built-in Windows command: IPConfig.
Guy's Get-IPConfig Function
- Planning the Get-IPConfig Function
- Example: PowerShell Function Get-IPConfig
- Learning Points for Creating a PowerShell Function
- Problems of Creating a PowerShell Function
♣
Planning the Get-IPConfig Function
The concept is to enhance Microsoft's original IPConfig command by creating parameters such as -MAC (Physical address: 20-CF-30-3A-B4-72), or -IP, which display just the dotty dot numbers, for example: 192.168.1.40.
1) Top Section – Help and Description
There is no need to worry about <# ….Adding Comments… #> until the end of the project.
2) PowerShell Parameters
Param(
List of parameters, for example $MAC. My advice is tackle the parameters section once you have the basic Process {IPConfig} working.
3) Process: Keyword, Main Section
This is the engine of your script. It this example I have created a series of:
If … ElseIf … statements, which refine the base IPConfig command.
4) Begin and End
These 2 keywords introduce optional sections, which could introduce one-off code.
Example: PowerShell Function Get-IPConfig
My mission is to provide a real-life example of how to create a PowerShell function.
Function Global:Get-IPConfig {
<#
.SYNOPSIS
PowerShell function to encase Microsoft's IPConfig
.DESCRIPTION
Includes parameters to filter MAC and IPv4 and IPv6 addresses
.EXAMPLE
Get-IPConfig -MAC
#>
[cmdletbinding()]
Param (
[Switch]$IP,
[Switch]$Mac,
[Switch]$All
)
Begin {
Clear-Host
} # End of small begin section
Process {
If($Mac) {
IPConfig -all | Select-String "Physical"
} # End of If
ElseIf($IP) {
IPConfig -all | Select-String "IPv"
}
ElseIf($All) {
IPConfig -all
} # End of ElseIf
Else {
IPConfig
} # End of Else
} # End of Process Section
End {
"`n " + (Get-Date).DateTime
} # End of 'End' section
} # End of Get-IPConfig Function
3 Commands to try
- Get-IPConfig -MAC
- Get-IPConfig -IP
- Help Get-IPConfig -full
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
Learning Points for Creating a PowerShell Function
- From scripting point of view, [Switch]$MAC enables you to oscillate between running the basic command, and modifying the code as laid out in the IF($MAC) {Swap this code} construction.
- From a user's point of view think of appending a -switch so that you can filter the output, for example:
IPConfig -MAC - In addition to providing the function's construction, PowerShell brings Select-String to this project, and thus enhances the underlying IPConfig command.
- The hidden agenda of this function is to illustrate how PowerShell can run native operating system commands such as IPConfig.
Problems of Creating a PowerShell Function
Firstly, there is the psychological problem that you can use Windows PowerShell perfectly well without bothering to create functions. Secondly, there are so many terms vying for your attention, for example: param, process and synopsis. Thirdly the syntax is fiddly; beginners get in a twist with different types of bracket they need to create a PowerShell function.
There is good news, creating a PowerShell is satisfying, and when you are starting out just ignore factors such writing <# comments #> if you want to concentrate on the action of the 'Process' section.
My technique was to learn by doing, building on success, gradually coming to embrace terms such as Global, [Switch] and to understand the significance of the brackets and the punctuation.
More About IPConfig Cmdlets
Another technique is to use a cmdlet called Get-NetIPConfiguration which comes with Windows 8.
See more on PowerShell function parameters ยป
Summary of Creating a PowerShell Function Get-IPConfig
The idea of this function is to enhance the built-in command IPConfig by creating parameters that filter the output to just display the MAC address or list only the IP addresses.
If you like this page then please share it with your friends
See More PowerShell Function Parameters
• Scripting PowerShell Function • PowerShell Function Clear-Recyclebin • PowerShell Get-File
• Create PowerShell Function Get-ServiceStatus • PowerShell Function Get-Driver • PowerShell Outlook
• Show-BalloonTip • PowerShell Function Get-IPConfig • Free Permissions Analyzer
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.