1

I needed to get the MAC address of several PCs as well as there IPs.

I came across this command over PowerShell to get the MAC address

Get-NetAdapter -Name "*Ethernet*","*Wi-Fi*"| Select Name,MacAddress

I had to use a different Cmdlet to get the IP

Get-NetIPAddress -InterfaceAlias "*Ethernet*","*Wi-Fi*" -AddressFamily IPv4 | Select InterfaceAlias, IPAddress

The problem arouse when I tried to run them in PowerShell file such as example.ps1.

Get-NetIPAddress -InterfaceAlias "*Ethernet*","*Wi-Fi*" -AddressFamily IPv4 | Select InterfaceAlias, IPAddress;
Get-NetAdapter -Name "*Ethernet*","*Wi-Fi*"| Select Name,MacAddress;
pause;

The result would come as follows and not both cmdlets would run. It was always the first out that ran and it was always after the pause. It drove me nuts

Press Enter to continue...: 

InterfaceAlias IPAddress     
-------------- ---------     
Ethernet       255.255.255.255
Wi-Fi          255.255.255.255

Question

How do I make both cmdlets run in PowerShell script and see the outputs? I want them to execute in order and have the pause happen at the end

I can get to run in a bat file if I just add powershell -Command "PS_COMMAND_HERE"

3 Answers 3

5

Note that Get-NetIPConfiguration has both the IPv4Address and MacAddress. The IPv4 address is normally an array.

Get-NetIPConfiguration | 
  select @{n='IPv4Address';e={$_.IPv4Address[0]}}, 
         @{n='MacAddress'; e={$_.NetAdapter.MacAddress}}

outputs

IPv4Address      MacAddress
-----------      ----------
182.16.183.105   9C-89-A5-B6-A6-30
173.18.1.1       10-15-5D-4C-E9-C2
1

You don't really need that Out-Host, since that is the PowerShell default. You can also do this in one line.

All together

Clear-Host
Get-NetAdapter | 
ForEach-Object {
        $PSitem | 
            Select-Object -Property Name, InterfaceDescription, ifIndex, Status, 
            MacAddress,  LinkSpeed,
            @{
                Name       = 'IPAddress'
                Expression = {(Get-NetIPAddress -InterfaceIndex ($PSItem).ifindex).IPv4Address}
            }
} | 
Format-Table -AutoSize

Or out to a file

Clear-Host
Get-NetAdapter | 
ForEach-Object {
        $PSitem | 
            Select-Object -Property Name, InterfaceDescription, ifIndex, Status, 
            MacAddress,  LinkSpeed,
            @{
                Name       = 'IPAddress'
                Expression = {(Get-NetIPAddress -InterfaceIndex ($PSItem).ifindex).IPv4Address}
            }
} | 
Export-Csv -Path 'D:\Temp\NicDetails.csv'
2
  • This is the modification I needed ``` Clear-Host Get-NetAdapter -InterfaceAlias "Ethernet","Wi-Fi" | ForEach-Object { $PSitem | Select-Object -Property Name, InterfaceDescription, Status, MacAddress, LinkSpeed, @{ Name = 'IPAddress' Expression = {(Get-NetIPAddress -InterfaceIndex ($PSItem).ifindex).IPv4Address} } } | Format-Table -AutoSize ``` Commented Sep 29, 2022 at 14:01
  • 1
    No worries. Glad it worked out. Remember to help folks follow your use case, it's best to always update your original post with your new info. Especially if it is code, vs putting code in comments. It makes it far easier for others to follow and assist as needed.
    – postanote
    Commented Sep 29, 2022 at 17:50
0

The way I ended up fixing it was through something I Found in the link below Not showing Table of Objects until after Pause

I just needed to edit the code as follows and added an Out-Host

Get-NetIPAddress -InterfaceAlias "*Ethernet*","*Wi-Fi*" -AddressFamily IPv4 | Select InterfaceAlias, IPAddress |Out-Host;
Get-NetAdapter -Name "*Ethernet*","*Wi-Fi*"| Select Name,MacAddress | Out-Host;
pause;

The output looks as follows now:

InterfaceAlias IPAddress     
-------------- ---------     
Ethernet       255.255.255.255
Wi-Fi          255.255.255.255  



Name     MacAddress       
----     ----------       
Wi-Fi    FF-FF-FF-FF-FF-FF
Ethernet FF-FF-FF-FF-FF-FF


Press Enter to continue...:

Please note that I was testing using the WINDOWS POWERSHELL ISE

If we want to have one table that has both, we can do it as follows. This is thanks to @PostNote's answer

Clear-Host
Get-NetAdapter -InterfaceAlias "*Ethernet*","*Wi-Fi*" | 
ForEach-Object {
        $PSitem | 
            Select-Object -Property Name, InterfaceDescription, Status, 
            MacAddress,  LinkSpeed,
            @{
                Name       = 'IPAddress'
                Expression = {(Get-NetIPAddress -InterfaceIndex ($PSItem).ifindex).IPv4Address}
            }
} | 
Format-Table -AutoSize

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.