Guy recommends :
Free Permissions
Analyzer Tool

Solarwinds Free Download of Permissions Analyzer

 

View the effective permissions for a folder or shared drive. Free download try it now!


PowerShell: More Flexible than Ipconfig

PowerShell Win32_NetworkAdapterConfiguration:
More flexible Than Ipconfig

Scripting WMI objects with PowerShell is a particularly productive area.  On this page I will illustrate PowerShell's capabilities by creating a script which is more flexible than Ipconfig.

PowerShell Win32_NetworkAdapterConfiguration Topics

 ♣

Our Mission

On this page our mission is to extract TCP/IP information from a machine's network adapters using PowerShell.  But first a question: 'Is Guy reinventing the wheel?  Can Ipconfig, with its numerous switches, provide the same information?' 

To answer this question is easy, all you have to do is compare Ipconfig with my PowerShell scripts, even better, compare Ipconfig with YOUR scripts.  If I could sum up the benefit of PowerShell over Ipconfig, in one word, that word would be flexibility.  For example, you can customize which TCP/IP properties to display, and in addition, you can interrogate the network adapters on other machines.  Can Ipconfig do that?  I have not found away.

PowerShell Objectives

WMIObject -List  (parameter to enumerate all possible object classes)
WMIObject | Get-Member (discover which properties suit our mission)
PowerShell: More flexible than Ipconfig.  (Use the 'Where' clause to filter the output)
Also remember that you could also run Ipconfig itself from the PowerShell command line.

Guy's Advice

Either work through my learning progression by starting with Objective 1 (recommended), or else if you are in a hurry, cut to the chase, and head for Objective 3 PowerShell: More flexible than Ipconfig.

PowerShell Pre-requisites and Checklist

In the case of Windows 7 and later, you don't need to download any extra files, just: 'Add Feature' --> Windows PowerShell.  However, for older operating systems, there are different versions of PowerShell for XP, Windows Server 2003 and Vista.  For such legacy systems only, you need to download PowerShell from Microsoft's site.

Once you have installed PowerShell 2.0 or later, I recommend choosing the ISE (Integrated Scripting Engine) version, it will save buying a text editor.

Objective 1) - List WMI Objects

Here is a cmdlet which identifies all the Network WMI objects.

$colItems = Get-WmiObject -List | where {$_.name -Match "network"}
$colItems | Format-Table name

Learning Points

Note 1: Because PowerShell is so simple, yet so efficient, we need fewer commands than for a corresponding VBScript.  Indeed, the bare minimum to get started, and display all the WMI objects is these 8 letters with a dash in the middle:
gwmi -List     # gwmi is an alias for Get-WmiObject

Note 2: Observe the (|) pipe.  You will use this symbol hundreds of times in PowerShell, what happens is the output of the wmiobject list is pumped into the 'where' clause, which pulls out all the entries that contain the word "network".  My thinking was Get-WmiObject -List produces too many objects.

Note 3: I deliberately don't use many aliases in my scripts, but I make an exception for ft (Format-Table) because it's so useful for controlling the display of PowerShell's output.  Do try the above script with, and without, the last word, 'name'.

Guy Recommends: Free WMI Monitor for PowerShellSolarwinds Free WMI Monitor for PowerShell

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft's operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try - it's free.

Download your free copy of WMI Monitor

Objective 2) - WMIObject | Get-Member (Discover which properties to use in our mission)

This script identifies the properties available for the object: Win32_NetworkAdapterConfiguration.  Here below is a classic example of the Get-Member construction.

$colItems = Get-WmiObject -class "Win32_NetworkAdapterConfiguration"
$colItems | Get-Member -Membertype Property

Learning Points

Note 1: Observe how -class homes in on the object that we are interested in, namely:
Win32_NetworkAdapterConfiguration.

Note 2: If you wished to manipulate the TCP/IP settings, you could investigate which methods are available by substituting 'Method' for the last word, 'Property'.  Alternatively, you could omit the '-Membertype Property' phrase altogether.

Objective 3) - PowerShell: More flexible than Ipconfig

Here is the main script illustrating PowerShell's ability to display information about your TCP/IP properties.

# PowerShell cmdlet to interrogate the Network Adapter
$strComputer = "."
$colItems = Get-WmiObject -class "Win32_NetworkAdapterConfiguration" `
-computername $strComputer | Where {$_.IpEnabled -Match "True"}
foreach ($objItem in $colItems) {
   Clear-Host
   Write-Host "MAC Address: " $objItem.MACAddress
   Write-Host "IPAddress: " $objItem.IPAddress
   Write-Host "IPAddress: " $objItem.IPEnabled
   Write-Host "DNS Servers: " $objItem.DNSServerSearchOrder
   Write-Host ""
}

Note 1: The properties that I have chosen are not important.  This is just an example to get you started; it would make my day if you substituted properties that you researched from Example 2: Get-Member, for example, .DefaultGateway or .IPSubnet instead of .MacAddress.

Note 2:  My old friend 'Barking' Eddie thought that my printer needed cleaning when he saw ` in my script.  Actually this character (`), found at the top left of the keyboard, is called a backtick.  What the backtick does is tell PowerShell - 'This command continues on the next line'.  Just to be clear, this character corresponds to ASCII 096, and is not a misplaced comma!

Note 3: To loop though all the network adapters, I chose the ForEach construction.  In PowerShell in general, and ForEach in particular, the type of bracket is highly significant; (ellipses for the condition) and {curly for the block command}.  See more on foreach.

Note 4:  Most of my scripts employ the 'Where' clause to filter the output.  In this case I wanted to discard data concerning any virtual network cards.

Note 5: I almost forgot, $strComputer controls which computer you are analysing.  Again, it would make my day if altered "." to the hostname of another machine on your network.

Guy Recommends: SolarWinds Free Network Bandwidth MonitorFree Real-Time Bandwidth Monitor

This freeware monitor is great for checking whether your network's load-balancing is performing as expected, for example, are two interfaces are getting about equal traffic?

It's easy to install and straightforward to configure. You will soon be running tests to see how much network bandwidth your applications consume.

The GUI has a lovely balance between immediate network traffic data in the middle, combined with buttons to seek related data and configuration settings. Give this monitor a try, it's free! 

Download your free network bandwidth monitor

If you need more comprehensive network analysis software:
Download a free trial of NPM (Network Performance Monitor)

Create a Function Called Get-IPConfig

My purpose of this script is to wrap Ipconfing in a PowerShell function.

Function Global:Get-IPConfig {
Param (
[Switch]$IP,
[Switch]$Mac,
[Switch]$All
     )
Begin {
Clear-Host
   }
Process {
If($Mac) {
IPConfig -all | Select-String "Physical"
  }
ElseIf($IP) {
IPConfig -all | Select-String "IPv"
  }
ElseIf($All) {
IPConfig -all
  }
Else {
IPConfig
  }
}
End {
"`n " + (Get-Date).DateTime
      }
}

How the function works

  • Get-IPConfig -MAC
  • Get-IPConfig -IP

See more on how to create Get-Ipconfig

Summary of PowerShell: More flexible than Ipconfig

The aim of this script is to show you that PowerShell can not only mimic Ipconfig, but also exceed its capabilities.  In particular, I wanted to show how PowerShell could customise list of TCP/IP properties in its output.  It would also be useful to create a script which can display TCP/IP information from other machines on the network.

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.

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor for PowershellSolarwinds WMI Monitor

Windows Management Instrumentation (WMI) is most useful for PowerShell scripting.

SolarWinds have produced this Free WMI Monitor to take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2013 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: