PowerShell: Get MAC Address of Local Machine

The below PowerShell code will return the MAC address for the local machine. I’ve also added a few other options on what I was needing to accomplish at the time. We needed a way to send an email of the MAC address for the local machine on boot. This would allow us to set a DHCP reservation to sorta control machines allowed to VPN into the network.


Get All MAC’s and Descriptions
Get-WmiObject win32_NetworkAdapterConfiguration | select macaddress, description | format-table -autosize
Check if file exists, get MAC, Filter only specific NICs, clear out white space email, email MAC with PC name
IF (!(Test-Path C:\Windows\Temp\mac.txt)){
Get-WmiObject win32_<wbr />networkadapterconfiguration | select macaddress | format-table macaddress, Handles -AutoSize -HideTableHeaders > C:\Windows\Temp\mac.txt
get-content C:\Windows\Temp\mac.txt | Select-String "00:0C:29" > C:\Windows\Temp\mac2.txt
(gc C:\Windows\Temp\mac2.txt) | Foreach {$_.TrimEnd()} | where {$_ -ne ""} > C:\Windows\Temp\$($env:Computername).txt
Send-MailMessage -SmtpServer smtp.gmail.com -Port 587 -Credential $creds -UseSsl -To youremail@gmail.com -From email@gmail.com -Subject "subject here" -Body "body here" -Attachments C:\Windows\Temp\$($env:Computername).txt
User Review
0 (0 votes)