Hyper-V PowerShell Snippets

This post is in the category: Scripts

Short scripts - usually bash or something else Linux-y

These are some PowerShell snippets that I use to create and manage VMs. I am very new to PowerShell…like my first day using it…ever. These are more notes for me to remember important commands.

For all examples, assume a VM with the name of dev-vm.

First things first – the VM needs to be created:

PS C:\> New-VM -Name dev-vm -MemoryStartupBytes 1GB -NewVHDPath "test-vm.vhdx" -NewVHDSizeBytes 12GB -SwitchName Intnet

More CPU Power! (Only one CPU core by default):

PS C:\> Set-VMProcessor dev-vm -count 2

Remove all Network Adapters:

PS C:\> Remove-VMNetworkAdapter -VMName dev-vm

Add a Legacy Network Adapter, useful for PXE booting. The name of the network that boots from my internal deployment server is Intnet. Your switch name will vary:

PS C:\> Add-VMNetworkAdapter -VMName dev-vm -IsLegacy $true -SwitchName Intnet

Add a Network Adapter, done after a PXE install is completed or not required. Your SwitchName will likely vary here as well:

PS C:\> Add-VMNetworkAdapter -VMName dev-vm -SwitchName External-Wired

Power on a VM:

PS C:\> Start-VM –Name dev-vm

Gracefully shutdown a VM.

PS C:\> Stop-VM –Name dev-vm

Give the VM five minutes to shutdown, and then do a hard power off.

PS C:\> Stop-VM -Force –Name dev-vm

Do a hard power off.

PS C:\> Stop-VM -TurnOff –Name dev-vm

Reference TechNet Article

This entry was posted in Scripts on by .

About Andrew Wells

I have been developing on the LAMP stack since about 2006. I run Ubuntu XFCE on my desktop and have a history of managing Ubuntu and CentOS servers. I code web applications mostly in PHP but have experience with other languages as well. When I'm not working, I can be found working in my home lab or out snowboarding, hiking, camping, or biking depending on the season.

Leave a Reply

Your email address will not be published. Required fields are marked *