selecting and piping a value of a property into another command RRS feed

  • Question

  • Hello,

    I would like to use the command New-VMSwitch to create a switch based on a particular network adapter.

    if I run Get-NetAdapter it will output all my network adapters, this includes the Name and MacAddress properties.

    if i run Get-NetAdapter | fl -Property Name this will output the name. same as if I use MacAddress, or MacAddress,Name

    my problem is that I don't know the network adapter name in advance (I want to run this autonomously) but I will know the MAC address. using the New-VMSwitch command takes the -NetAdapterName as a string, which would come from the Name of the Get-NetAdapter, the New-VMSwitch does not seem to accept a MacAddress as a parameter. So, somehow I need to get the network adapter name derived from the MAC Address (which I already know beforehand), into the -NetAdapterName for the New-VMSwitch command.

    I really don't know much about PowerShell, and I have tried to look this up but I couldn't easily relate to the examples shown.

    many thanks


    Tuesday, July 22, 2014 12:49 PM


All replies

  • Start by learning the basics of PowerShell.  Your question is answered in chapter 3 of most basic books.

    Get-NetAdapter|?{$_.MacAddress -eq '68-94-23-A3-97-FD'}


    Tuesday, July 22, 2014 12:56 PM
  • Hi Steve,

    Here's one way you can accomplish this:

    $macToFind = '00-11-22-33-44-55'
    $nicName = Get-NetAdapter | Where { $_.MacAddress -eq $macToFind } | Select -ExpandProperty Name

    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    • Marked as answer by Milkientia Tuesday, July 22, 2014 1:19 PM
    Tuesday, July 22, 2014 12:57 PM
  • That is exactly what I was after, clear and it worked. By providing a proper answer like this it helps me understand more about how this sort of thing works. sure i could read plenty of books and technical articles, which i do already - but PowerShell is a lesser used function in my job at the moment and I have to priorities other technical reading over it for the time being.

    Thanks ever so much for that Mike.


    Tuesday, July 22, 2014 1:19 PM
  • Two quick things:

    Format-List (aliased as fl) is what I refer to as a "terminating cmdlet" aka the last one in the pipeline. For some reason you cannot select properties after using FL. If you want to select properties, use Select-Object instead.

    You might benefit from some filtering in this script or others. Check out Where-Object and it's corresponding help by running the command Get-Help Where-Object.

    If you choose to use Select-Object and Where-Object note that the desired order of operations is Where-Object prior to Select-Object. Basically the opposite of common SQL queries.

    Like so:

    $myResults | Where-Object { $_ -eq "targetvalue" } | Select-Object <property>

    Tuesday, July 22, 2014 1:26 PM
  • Steve, when you get some time play around with Where-Object and Select-Object as Mike's code demonstrates. I tried the approach of killing myself by pouring through books and it wasn't until I started playing around with those two cmdlets did I really learn the pipeline. Try them on Active Directory, CSV files, etc.

    Tuesday, July 22, 2014 1:28 PM
  • Cheers for that, it's all a learning curve, I am slowly getting to do more PowerShell and as I automate things like in my last job I am trying to aim for PS over VBScript. thanks for your help guys.


    Wednesday, July 23, 2014 7:11 AM