[SOLVED] Nic selection in Powershell

Hello y’all,
So I’ve been trying to select the first network card on my server and setting that as a variable to allow the out put to be used on a different command. I’m able to get the first nic but when I try to us it in the other command I get an error stating “Sequence contains no matching element”

My goal is to automate the creation of a VM in Hyper-V. First step is to make the Virtual switch.
The command I’m using is “$NIC = Get-NetAdapter | Select-Object -First 1 -Property InterfaceDescription”. This sets the “$NIC” to the output of the command. I then try to use the above in “New-VMSwitch -Name “VM Switch” -NetAdapterInterfaceDescription $NIC” which is when I get the following error :

New-VMSwitch : Sequence contains no matching element
At line:2 char:1

  • New-VMSwitch -Name “VM Switch” -NetAdapterInterfaceDescription $NIC
  •   + CategoryInfo          : InvalidOperation: (:) [New-VMSwitch], VirtualizationException
      + FullyQualifiedErrorId : InvalidOperation,Microsoft.HyperV.PowerShell.Commands.NewVMSwitch
    
    

I think the issue is the the “InterfaceDescription” showing up above the nic I need but I can’t seem to find a way to remove that from the output. Is it even possible to remove that bit of the out put?
PS: I’m not a wiz at powershell.

I found a way to get the description off of the output but I’m still getting the same error. Think I have to revisit the help pages for the part of the command.
I was able to remove the description by piping the “select-object” to “format-tables” .
Get-NetAdapter | Select-Object -First 1 -Property InterfaceDescription | Format-Table -HideTableHeaders

I think you need

$Nic.InterfaceDescription rather than $Nic as $Nic is an Object and not a String in this case but $Nic.InterfaceDescription is the name of the adapter as a string

cant verify as i dont have Hyper-V installed

1 Like

THANK YOU!!!
Yes that worked.
Where did you read/learn about that? I’m currently going through “Learn Windows Powershell in a Month of lunches” but I’d like more reading material.
I am using the Powershell ISE and I did see the .InterfaceDescpition option after I typed out the $NIC. Guess I should work more in the actual console then the scripting portion.
Thanks again spider099!

yes i have the same book

the clue is when you type

$nic in the console - is what it shows

also it will depend what you assign to the variable and also some of the time PS does things by default so the output can be limited by PS when there are a lot more object properties

yes use the ISE or i am looking at using Visual Studio Code and add the PS plugins

Have fun