Using Powershell to gather MAC Addresses of Network cards EASILY – Part 2

 PowershellBIG

 

So last time we left you with a partially useful Powershell line.  The ability to type in a computer name into a variable and have it list all the Physical network cards attached to the System via WMI (Windows Management Instrumentation).

The end result of this desired operation would be to have a list of all the MAC addresses on your PC’s.   For what purpose?  Possibly setting up SCCM 2007 or maybe even to aid in securing DHCP by having a list of MAC addresses in use and only only handing out IP’s to registered network cards.

Or maybe you just like collecting MAC Addresses 🙂

But to make this truly useful, we need a list of computers.  Now if I (or you) have to go to each computer to GET it’s name, that completely defeats the purpose.  But if you have Active Directory on your site this really gets easy.   Any version will do for Powershell.

 

So our first piece involves having the FREE Quest Active Roles Management Shell for Active Directory for Powershell.  We’re going to use the “GET-QADCOMPUTER” to get a list of systems in Active Directory, because well, that’s GET-QADCOMPUTER DOES and does very well and simply.

 

GET-QADCOMPUTER

 

That of course will dump a list from 3 to 300 lines of computer names depending on the size of your network.  But we don’t want to see the pretty list.  We need that list to work for us.  So we’re going to store that information away in a Powershell variable.

 

$COMPUTERLIST=GET-QADCOMPUTER

 

Once we have this list we can select and pipe each item from this list into the Powershell commandlet from before

—— PRINTMACLIST.PS1 ——

$COMPUTERLIST=GET-QADCOMPUTER

FOREACH ($PC IN $COMPUTERLIST)
{

Get-WmiObject WIN32-NETWORKADAPTER -computer $PC.NAME | where { $-.PhysicalAdapter.CompareTo($FALSE) } | Format-table @{Label="ComputerName"; Expression={$PC.NAME}}, NAME, MACADDRESS,

}

——– END OF SCRIPT ———–

 

Now this is all fine and dandy.  A printed report on the screen.   But the whole point of Powershell and Computers and Life in general is try and SAVE work, like avoiding typing in that silly list.   So instead of using “FORMAT-TABLE” we’re going to “SELECT” from the OBJECT the PROPERTIES we want so we can EXPORT it to a CSV (Anybody see where I’m going with this?  Yeah I’m that obvious eh?)

 

So the same script but swap out “FORMAT-TABLE” with “SELECT-OBJECT” which allows out to pick out what we want.  Then we’ll use EXPORT-CSV to pipe all that output into a nice file which should at LEAST save on typing

 

—— EXPORTMACLIST.PS1 ——

$COMPUTERLIST=GET-QADCOMPUTER

FOREACH ($PC IN $COMPUTERLIST)

{

Get-WmiObject WIN32-NETWORKADAPTER -computer $PC.NAME | where { $-.PhysicalAdapter.CompareTo($FALSE) } | select-object NAME, MACADDRESS, @{Label="ComputerName"; Expression={$PC.NAME}} | Export-csv C:MACADDRS.CSV

}

——– END OF SCRIPT ———–

 

And now what you’ll have is an easy way to pull out the MacAddresses, list of Physical network cards INCLUDING the Computer name on each line.   The interesting part is because this is mostly based on WMI you could easily add on Filters to only show computers which are Workstations.  And With GET-QADCOMPUTER you can even filter specific OU’s containing computers.

 

Enjoy the Power

It’s Easy and It’s Free

 

Sean

The Energized Tech

Leave a Reply