12 Hyper-V Powershell Commands

Estimated reading: 12 minutes 126 views

Essential Hyper-V PowerShell Commands

Managing Hyper-V environments can be a challenging task. Thus, you need a powerful management tool which allows you to configure and control your virtual infrastructure from a central console. Even though you can use advanced GUI tools (Hyper-V Manager, Server Manager, or System Center Virtual Machine Manager), you still won’t be able to fully enjoy the Hyper-V experience and explore the hypervisor’s functionality without using PowerShell. Unlike the aforementioned tools, PowerShell allows you to automate all core tasks, thus providing you with more flexibility and efficiency.

Today’s blog post discovers how PowerShell works, which Hyper-V PowerShell commands can improve your virtual machine (VM) performance, and how this CLI tool can considerably simplify the process of Hyper-V management. Here is a list of Hyper-V PowerShell commands which every IT administrator should know:

  1. Get-Command
  2. Get-Help
  3. Get-VM
  4. Start-VM
  5. Stop-VM
  6. Checkpoint-VM
  7. Test-VHD
  8. Measure-VM
  9. Get-VMNetworkAdapter
  10. Update-VMVersion
  11. Get-Process
  12. Where-Object

Moreover, read this blog post to find out how NAKIVO Backup & Replication can help you protect your Hyper-V environment and at the same time save your time, money, and effort.

 

What Is PowerShell?

PowerShell is a task automation and configuration management framework developed by Microsoft, which includes a command-line shell and associated scripting language built on the .NET Framework and .NET Core. This tool was specifically designed for system administrators and it includes an interactive prompt and a scripting environment that can be used independently or in combination.

PowerShell commands, also known as cmdlets, can be used to automate various tasks by arranging several cmdlets in a single PowerShell script. Microsoft has developed more than one hundred core PowerShell commands which can be used for performing basic or complex tasks.

However, PowerShell is an open-source tool, meaning that new cmdlets can be added by any third-party developer. Thus, anyone can contribute to the Microsoft’s PowerShell code base by sharing their own PowerShell commands on GitHub.

 

What Can PowerShell Do?

New and inexperienced Hyper-V users generally prefer to run GUI tools because it is much easier to find required features and settings as well as notice any issues using a graphical interface. However, GUI tools do not enable automation, meaning that to apply changes to all VMs you would have to configure each Hyper-V VM separately. For example, after creating a new VM, you would have to manually update some of its default settings.

PowerShell can help Hyper-V administrators rapidly automate tasks that manage operating systems (Linux, macOS, and Windows) and associated processes. Unlike GUI tools, PowerShell allows you to manage the entire system using only the command line.

Moreover, you can save required Hyper-V PowerShell commands into a text file (which has the .ps1 extension) and then run this script to perform certain tasks. What’s more, you can schedule a PowerShell script to be launched at a particular time using the Task Scheduler.

How to Open PowerShell

To open PowerShell in a GUI-based OS, you need to click Start and type PowerShell in the search bar. Right-click the PowerShell icon and select Run as Administrator to open PowerShell in elevated mode.

 

If you run Windows Server Core or Hyper-V Server, then you have to type PowerShell in the Command Prompt and press Enter. After that, a Windows PowerShell session in a Command Prompt window should start automatically. A PS prefix is then added to the command prompt to indicate that you have started a Windows PowerShell session.

Best Hyper-V PowerShell Commands

After you have discovered what PowerShell is and how it works, it is time to learn how to use Hyper-V PowerShell commands to your advantage. There are a number of effective Hyper-V PowerShell commands that can make the process of managing Hyper-V easy and simple.

 

Let’s now learn about the different possibilities that Hyper-V PowerShell commands can open for your virtual infrastructure.

 

Get-Command

This Hyper-V PowerShell command is especially helpful for users who are new to PowerShell and are not aware of which cmdlets should be used and how. By running the Get-Command cmdlet, you can see the full list of all available cmdlets, functions, and aliases installed on the computer.

However, if you wish to narrow down your search and get the list of Hyper-V PowerShell commands that match specific criteria, you can use the Get-Command *-VM cmdlet to find a list of commands that allow you to control and configure VMs. By changing the VM parameter to VHD, you can get the full list of cmdlets used for managing virtual hard disks. Thus, you can try out various search parameters to get the results you need.

Get-Help

After you have decided which Hyper-V PowerShell command you would like to use, you can learn more about the cmdlet. For this purpose, you can use the Get-Help cmdlet. For example, to find out more about the Get-VM cmdlet, type the following:

Get-Help Get-VMGet-VM

 

Get-VM

The following Hyper-V PowerShell command allows you to see all of the VMs available on one or more Hyper-V hosts. To see all VMs on the local Hyper-V host, you should run the Get-VM cmdlet.

On the PowerShell screen, you can see the list of available VMs, including their name, state, CPU usage, memory assigned, uptime, status, and version.

 

Start-VM

This particular cmdlet helps you start a VM of your choice. For this purpose, run the following Hyper-V PowerShell command:

 

Start-VM -Name 'Windows Server 2016'

As a result, the VM named Windows Server 2016 should be started on your local Hyper-V host. Let’s check to see if the VM has actually started by running the Get-VM cmdlet.

 

Moreover, you can use a modified version of the Start-VM cmdlet, which allows you to launch the VMs only if their names start with Hyper-V:

Start-VM -Name Hyper-V*

As you can see, the VM named Hyper-V Server Desktop has successfully started. However, another VM (Hyper-V Server 2012) has already been in a running state and PowerShell notifies you of this fact.

Stop-VM

The Stop-VM cmdlet allows you to shut down or turn off a selected VM. For this purpose, you can run the following cmdlet:

Stop-VM -Name 'Windows Server 2016'

 

As you can see, the selected VM has been successfully shut down through the guest OS.

Another example is that you can turn off the VMs whose names start with Hyper-V:

Stop-VM -Name Hyper-V* –TurnOff

Stop-VM -Name 'Windows Server 2016' –Force

Note that forced shutdown is similar to pulling the power on a physical machine, meaning that it can result in a loss of unsaved application data. In this case, it is recommended that you save the VM in order to avoid potential data loss. For this purpose, you need to run the Save-VM cmdlet, which is equivalent to hibernating a physical computer:

Save-VM -Name 'Windows Server 2016'

If you choose to save the VMs whose names start with Hyper-V, run the following Hyper-V PowerShell command:

Get-VM -Name Hyper-V* | Save-VM

 

Checkpoint-VM

Another Hyper-V PowerShell command which is crucial for managing your virtual environment is the Checkpoint-VM cmdlet. The Checkpoint-VM cmdlet helps you create a Hyper-V snapshot (also known as a Hyper-V snapshot) of a selected VM. Run the following cmdlet to create a checkpoint named Update1 of the specific VM (Windows Server 2016).

 

Checkpoint-VM -Name 'Windows Server 2016' -SnapshotName Update1

To verify that the VM has been successfully created, use the following Hyper-V PowerShell command:

 

Get-VMSnapshot -VMName 'Windows Server 2016'

On the other hand, if you want to delete certain Hyper-V snapshots, you can use the Remove-VMSnapshot cmdlet. For example, if I want to delete all VM snapshots whose names start with Testing, I should run the following cmdlet:

 

Get-VM 'Hyper-V Server 2012' | Remove-VMSnapshot -Name Testing*

The screenshot above demonstrates the entire process of deleting the selected Hyper-V snapshots.

 

Test-VHD

This Hyper-V PowerShell command is intended for verifying that the virtual hard disk file actually exists and can be used for Hyper-V deployment. This cmdlet goes as follows:

 

Test-VHD -Path C:\Testing.vhd

In the case that the VHD path is valid, you would see the following output on your screen.

To learn more about managing virtual hard disks using PowerShell cmdlets, we recommend that you read our blog post which delves into essential basics of working with Hyper-V VHD and VHDX files.

 

Measure-VM

This Hyper-V PowerShell command allows you to gather resource utilization data for a specific VM. For example, run the following cmdlet to learn about CPU usage, RAM usage, disk capacity, and network traffic for the VM named Windows Server 2016:

 

Measure-VM -VMName 'Windows Server 2016'

Note that the Measure-VM cmdlet can report required data only after resource metering has been enabled on a selected VM. To enable resource metering, use the following Hyper-V PowerShell command:

 

Enable-VMResourceMetering -VMName 'Windows Server 2016'

Get-VMNetworkAdapter

Another helpful PowerShell cmdlet is Get-VMNetworkAdapter, which allows you to identify the virtual network adapters of the selected VM. For example, if you want to find virtual network adapters from all VMs as well as the management OS, you should run the following Hyper-V PowerShell command:

 

Get-VMNetworkAdapter –All

If everything is correct, you should see the following information on your PowerShell screen:

Using Hyper-V PowerShell commands, you can perform various simple as well as complex tasks with your Hyper-V network adapters. Check out one of our blog posts to find out which cmdlets can help manage and configure virtual network adapters in your Hyper-V environment.

 

Update-VMVersion

PowerShell has a cmdlet which allows you to update the version of the selected VM:

 

Update-VMVersion -Name 'TestVM'

After running the Hyper-V PowerShell command, the VM named TestVM can be updated to the current VM version.

 

Get-Process

This Hyper-V PowerShell command can help you see all the current processes on your local computer. After running the Get-Process cmdlet, you can get the following information on your screen:

This table includes the information about how many handles the process has opened; how much non-paged memory the process is using; how much pageable memory the process is using; the size of the working set of the process; how much virtual memory the process is using; how much the processor time has been allocated to all processors; the process ID; the process name.

To narrow down the search and find a specific process, you should type the process name or ID. For example, if you want to learn how Microsoft Word is currently working, you should look through the list of all the running processes and find the line with the information about Microsoft Word (i.e. WinWord), including its process ID. Using the process ID, you can run the following cmdlet to get the necessary data:

 

Get-Process -ID 8892

Furthermore, the data about the current WinWord processes can be presented in another format, i.e. as a list of properties. Run this cmdlet to get a more detailed output:

 

Get-Process WINWORD | Format-List *

After getting hold of the process data, you can run the following cmdlets:

Stop-Process, which allows you to stop one or more running processes.
Wait-Process, which allows you to wait for one or more running processes to be stopped before enabling any other input.
Start-Process, which allows you to start one or more processes on the local computer.
Debug-Process, which allows you to attach a debugger to one or more running processes on the local computer.
For example, if you want to launch the Notepad application from PowerShell, you should run the following cmdlet:

Start-Process -FilePath "notepad.exe"

Another example is when you have opened PowerShell in “normal” mode, but you need to run PowerShell in elevated mode to get access to administrative rights. In such case, run the following Hyper-V PowerShell command:

Start-Process -FilePath "powershell" -Verb RunAs

On the other hand, if one of the running processes stops responding, you can run the Stop-Process cmdlet to halt the failed application:

Stop-Process -Name “notepad”
In this case, all instances of the Notepad application on the local computer are stopped.

Each Notepad instance has its own process ID. If you want to stop a particular Notepad instance, you should use the ID parameter in the Stop-Process cmdlet. For this purpose, you first need to run the Get-Process cmdlet, find the required active processes, and identify the process ID that you want to stop.

Notepad process IDs (Hyper-V PowerShell Commands)

For example, you can stop the specific Notepad process by identifying its process ID and running the following Hyper-V PowerShell command:

Stop-Process -ID 5052

As a result, only the selected Notepad process should be stopped.

Where-Object

This Hyper-V PowerShell command allows you to filter pipeline objects and find the exact data that you need. In this case, you need to create a PowerShell script in which you specify which objects can be passed along the pipeline. Using various search criteria, you can find any object on the local computer.

For example, if you would like to locate the processes which have stopped running, use this cmdlet:

Get-Service | Where-Object {$_.Status -eq 'Stopped'}

Another example of using the Where-Object cmdlet is when you want to find a specific cmdlet. The following Hyper-V PowerShell command can help you find all cmdlets which start with Wait:

Get-Command | Where-Object {$_.Name -like '*wait*'}

Wait Cmdlets (Hyper-V PowerShell Commands)

The next Where-Object cmdlet can help you find a particular process based only on its name. By running the following command, you can get a list of all the processes whose names start with sys:

Get-Process | Where-Object {$_.ProcessName -Match "^sys.*"}

Filtering the processes (Hyper-V PowerShell Commands)

As you can see, the number of tasks which can be performed using these cmdlets is impressive and you should definitely pay more attention to Hyper-V PowerShell commands. PowerShell cmdlets allow you to perform various tasks with multiple VMs at the same time and explore the Hyper-V functionality to the fullest. Even though PowerShell might seem intimidating at first, once you learn how to work with it, you can quickly discover how to use Hyper-V PowerShell commands to your advantage.

 

Leave a Comment