Select the virtual switch using PS.

Doria 1,246 Reputation points
2021-07-21T20:50:57.533+00:00

Hi everyone!

How can I make in PS (PowerShell) the image action?

116881-image.png

Select the virtual switch for the VM.

Thanks.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Answer accepted by question author
  1. Mark E 161 Reputation points
    2021-07-21T21:48:56.347+00:00

    Use

    Connect-VMNetworkAdapter -vmname <VMName> -Name <NetworkAdapterName> -switchname <Switchname>

    So for a VM called test with a NIC called NIC1 connecting to ExternalNetwork

    Connect-VMNetworkAdapter -vmname test -Name NIC1 -switchname ExternalNetwork

    If you don’t know the name of the network adapter use;

    Get-VMNetworkAdapter -vmname test

    to get a list of adapters for that VM


1 additional answer

Sort by: Most helpful
  1. Anonymous
    2021-07-22T01:36:20.667+00:00

    Hi,

    If you would like to connect a virtual switch to a VM by PowerShell, you could use Connect-VMNetworkAdapter command.
    For your reference: https://learn.microsoft.com/en-us/powershell/module/hyper-v/connect-vmnetworkadapter?view=windowsserver2019-ps

    If you would like to disconnect a virtual switch by PowerShell, you could use Disconnect-VMNetworkAdapter command.
    For your reference: https://learn.microsoft.com/en-us/powershell/module/hyper-v/disconnect-vmnetworkadapter?view=windowsserver2019-ps

    If you would like to get virtual switches from the Hyper-V host, you could use Get-VMSwitch command.
    For your reference: https://learn.microsoft.com/en-us/powershell/module/hyper-v/get-vmswitch?view=windowsserver2019-ps

    If you would like to create a new switch, you could use New-VMSwitch command.
    For your reference: https://learn.microsoft.com/en-us/windows-server/virtualization/hyper-v/get-started/create-a-virtual-switch-for-hyper-v-virtual-machines#create-a-virtual-switch-by-using-windows-powershell

    If you would like to create a new VM with specific switch, you could use New-VM -Name <Name> -MemoryStartupBytes <Memory> -BootDevice <BootDevice> -VHDPath <VHDPath> -Path <Path> -Generation <Generation> -Switch <SwitchName>
    For your reference: https://learn.microsoft.com/en-us/windows-server/virtualization/hyper-v/get-started/create-a-virtual-machine-in-hyper-v#create-a-virtual-machine-by-using-windows-powershell

    Hope above information could help you.

    Thanks,

    ----------

    If the Answer is helpful, please click "Accept Answer" and upvote it. Thanks.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.