HyperV VM creation assign virtual switch with powershell

Bart Hofstede 21 Reputation points
2021-06-30T15:23:15.47+00:00

I'm testing with creating vm's and VM settings with powershell to do for some automation.

But i'm not able to acomplish something as simple as setting the correct virtual-switch for the VM :(

new-vm -Name "rr-rds2" -MemoryStartupBytes 45056MB -BootDevice NetworkAdapter -NewVHDPath "D:\hyperv\rr-rds2\Virtual Machines\rr-rds2.vhdx" -Path "D:\hyperv\rr-rds2\Virtual Machines\" -Generation 2 -NewVHDSizeBytes 250GB -SwitchName "VM's - RR"

When i'm removing this switch -SwitchName "VM's - RR" the new virtual machine is created with the network adaptper virtual switch status not connected.

What is the correct way to achieve this when creating the vm after the VM is created?

Hyper-V
Hyper-V
A Windows technology providing a hypervisor-based virtualization solution enabling customers to consolidate workloads onto a single server.
2,530 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,358 questions
0 comments No comments
{count} votes

Accepted answer
  1. Eleven Yu (Shanghai Wicresoft Co,.Ltd.) 10,671 Reputation points Microsoft Vendor
    2021-07-02T09:07:01.327+00:00

    Hi,

    Please try below command:

    New-VM -Name rr-rds2 -MemoryStartupBytes 45056MB -BootDevice NetworkAdapter -NewVHDPath D:\hyperv\rr-rds2\Virtual Machines\rr-rds2.vhdx -Path D:\hyperv\rr-rds2\Virtual Machines -NewVHDSizeBytes 250GB -Generation 2 -Switch VM's-RR

    I think you need to replace "SwitchName" parameter with "Switch" parameter. Also, please ensure your switch name is correct. There should not be spacing character in the name.

    If the problem persists, please take a screenshot of the command running result. Thanks.

    Thanks,


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

    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Eleven Yu (Shanghai Wicresoft Co,.Ltd.) 10,671 Reputation points Microsoft Vendor
    2021-07-01T01:43:27.447+00:00

    Hi,

    Could you clarify more about your requirement? What would you like to achieve when creating VM by PowerShell?

    If your PowerShell command does not include switch part "- switch <SwitchName>", the VM's networking configuration will be "not connected" by default.

    Please kindly check below article for reference.

    Create a virtual machine by using Windows PowerShell
    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

    Thanks,

    ----------

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

    0 comments No comments

  2. Bart Hofstede 21 Reputation points
    2021-07-02T08:19:50.4+00:00

    When i create the machine with the -SwitchName "VM's - RR"

    new-vm -Name "rr-rds2" -MemoryStartupBytes 45056MB -BootDevice NetworkAdapter -NewVHDPath "D:\hyperv\rr-rds2\Virtual Machines\rr-rds2.vhdx" -Path "D:\hyperv\rr-rds2\Virtual Machines\" -Generation 2 -NewVHDSizeBytes 250GB -SwitchName "VM's - RR"

    new-vm : Invalid query
    At line:1 char:1

    • new-vm -Name "VM's - RR" -MemoryStartupBytes 45056MB -BootDevice Networ ...
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : NotSpecified: (:) [New-VM], VirtualizationException
    • FullyQualifiedErrorId : Unspecified,Microsoft.HyperV.PowerShell.Commands.NewVM

    When i remove the -SwitchName "VM's - RR" the VM gets created with the network adaptper virtual switch status not connected. So this parameter is not correct. But i dont know why and how to correct this.

    0 comments No comments

  3. Bart Hofstede 21 Reputation points
    2021-07-02T10:35:18.407+00:00

    That's it. The parameter and the spaces aren't working. What is right command if i want to change it after the VM is created?

    0 comments No comments

  4. Eleven Yu (Shanghai Wicresoft Co,.Ltd.) 10,671 Reputation points Microsoft Vendor
    2021-07-05T01:13:07.583+00:00

    Hi,

    Do you mean you would like to connect the created VM to a switch by PowerShell Command?

    If so, the command can be as below:

    Get-VMNetworkAdapter -VMName rr-rds | Connect-VMNetworkAdapter -SwitchName VM's-RR

    For your reference:
    https://learn.microsoft.com/en-us/powershell/module/hyper-v/connect-vmnetworkadapter?view=windowsserver2019-ps

    Thanks,

    0 comments No comments