How To Start Hyper V Virtual Machine From Command Line
Hyper-V is a virtualization technology provided by Microsoft that enables you to create and manage virtual machines on Windows servers. While Hyper-V provides a graphical user interface for managing virtual machines, it is also possible to manage virtual machines from the command line. In this article, we will discuss how to start a Hyper-V virtual machine from the command line.
Step 1: Open Command Prompt as an Administrator
To start a Hyper-V virtual machine from the command line, you must first open Command Prompt as an administrator. To do this, follow these steps:
- Click on the Start button and type “cmd” in the search bar.
- Right-click on the Command Prompt application and select “Run as administrator”.
- If prompted for permission, click “Yes”.
Step 2: Navigate to the Hyper-V Directory
The next step is to navigate to the directory where Hyper-V is installed. The default location for Hyper-V on Windows Server 2012 and later is “C:\Windows\System32\WindowsPowerShell\v1.0\Modules\Hyper-V”.
To navigate to this directory, type the following command in Command Prompt:
cd C:\Windows\System32\WindowsPowerShell\v1.0\Modules\Hyper-V
Step 3: Check the Name of the Virtual Machine
Before starting a virtual machine from the command line, you must know the name of the virtual machine. To check the name of the virtual machine, use the following command:
Get-VM
This will display a list of all the virtual machines on the server along with their names.
Step 4: Start the Virtual Machine
Once you know the name of the virtual machine, you can start it from the command line using the following command:
Start-VM -Name <VirtualMachineName>
Replace <VirtualMachineName> with the name of the virtual machine that you want to start. For example, if the name of the virtual machine is “TestVM”, the command would be:
Start-VM -Name TestVM
This will start the virtual machine.
Step 5: Verify that the Virtual Machine has Started
To verify that the virtual machine has started, you can use the following command:
Get-VM -Name <VirtualMachineName> | Select-Object -ExpandProperty State
Replace <VirtualMachineName> with the name of the virtual machine that you started. This command will display the state of the virtual machine. If the virtual machine is running, the state will be “Running”.
Alternative Method: Using PowerShell Script
If you need to start a virtual machine from the command line frequently, you can create a PowerShell script to automate the process. Here’s how:
- Open Notepad or any text editor.
- Copy and paste the following code:
$vmName = "<VirtualMachineName>"
$vm = Get-VM -Name $vmName
Start-VM -VM $vm
- Replace <VirtualMachineName> with the name of the virtual machine that you want to start.
- Save the file with a .ps1 extension, for example, “StartVM.ps1”.
To run the script, open Command Prompt as an administrator, navigate to the directory where the script is saved, and type the following command:
powershell -ExecutionPolicy RemoteSigned -File .\StartVM.ps1
This will start the virtual machine specified in the script.
Here are a few examples of PowerShell commands that can be used to manage virtual machines:
Stopping a Virtual Machine
To stop a running virtual machine, use the following command:
Stop-VM -Name <VirtualMachineName>
This will stop the virtual machine with the specified name.
Restarting a Virtual Machine
To restart a virtual machine, use the following command:
Restart-VM -Name <VirtualMachineName>
This will stop and then start the virtual machine with the specified name.
Changing Virtual Machine Settings
To change the settings of a virtual machine, use the following command:
Set-VM -Name <VirtualMachineName> -MemoryStartupBytes <MemorySize> -ProcessorCount <NumberOfProcessors>
Replace <VirtualMachineName> with the name of the virtual machine that you want to change the settings for. Replace <MemorySize> with the desired amount of memory for the virtual machine in bytes (for example, 1 GB = 1073741824 bytes). Replace <NumberOfProcessors> with the desired number of virtual processors for the virtual machine.
Creating a New Virtual Machine
To create a new virtual machine, use the following command:
New-VM -Name <VirtualMachineName> -MemoryStartupBytes <MemorySize> -NewVHDPath <VHDPath> -NewVHDSizeBytes <VHDSize> -SwitchName <NetworkSwitchName>
Replace <VirtualMachineName> with the desired name for the new virtual machine. Replace <MemorySize> with the desired amount of memory for the virtual machine in bytes. Replace <VHDPath> with the path where the virtual hard disk (VHD) for the new virtual machine should be created. Replace <VHDSize> with the desired size of the VHD in bytes. Replace <NetworkSwitchName> with the name of the virtual switch that the virtual machine should use to connect to the network.
How to enable Hyper-V in Windows 10 using command line?
- Open an elevated Command Prompt window by right-clicking the Start menu and selecting “Command Prompt (Admin)”.
- Type the following command and press Enter to enable the Hyper-V feature:
DISM /Online /Enable-Feature /All /FeatureName:Microsoft-Hyper-V
- Restart your computer to complete the installation of the Hyper-V feature.
- Once your computer has restarted, open the Hyper-V Manager to create and manage virtual machines.
Alternatively, you can also use PowerShell to enable the Hyper-V feature. Here are the steps to follow:
- Open an elevated PowerShell window by right-clicking the Start menu and selecting “Windows PowerShell (Admin)”.
- Type the following command and press Enter to enable the Hyper-V feature:
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All
- Restart your computer to complete the installation of the Hyper-V feature.
- Once your computer has restarted, open the Hyper-V Manager to create and manage virtual machines.
Enabling Hyper-V using the command line is a quick and easy way to get started with virtualization on Windows 10. Whether you are a developer, IT professional, or just someone who wants to experiment with virtual machines, Hyper-V is a powerful tool that can help you get the job done.
Conclusion
Using PowerShell commands to manage Hyper-V virtual machines provides a fast and efficient way to perform management tasks without having to use the graphical user interface. Starting, stopping, and restarting virtual machines, changing virtual machine settings, and creating new virtual machines can all be accomplished using PowerShell commands. By learning to use these commands, Hyper-V administrators can save time and streamline virtual machine management on Windows servers. Starting a Hyper-V virtual machine from the command line is a quick and easy way to manage virtual machines on Windows servers. By following the steps outlined in this article, you can start a virtual machine from the command line and automate the process using a PowerShell script.
How To Start Hyper V Virtual Machine From Command Line
Hyper-V is a virtualization technology provided by Microsoft that enables you to create and manage virtual machines on Windows servers. While Hyper-V provides a graphical user interface for managing virtual machines, it is also possible to manage virtual machines from the command line. In this article, we will discuss how to start a Hyper-V virtual machine from the command line.
Step 1: Open Command Prompt as an Administrator
To start a Hyper-V virtual machine from the command line, you must first open Command Prompt as an administrator. To do this, follow these steps:
- Click on the Start button and type “cmd” in the search bar.
- Right-click on the Command Prompt application and select “Run as administrator”.
- If prompted for permission, click “Yes”.
Step 2: Navigate to the Hyper-V Directory
The next step is to navigate to the directory where Hyper-V is installed. The default location for Hyper-V on Windows Server 2012 and later is “C:\Windows\System32\WindowsPowerShell\v1.0\Modules\Hyper-V”.
To navigate to this directory, type the following command in Command Prompt:
cd C:\Windows\System32\WindowsPowerShell\v1.0\Modules\Hyper-V
Step 3: Check the Name of the Virtual Machine
Before starting a virtual machine from the command line, you must know the name of the virtual machine. To check the name of the virtual machine, use the following command:
Get-VM
This will display a list of all the virtual machines on the server along with their names.
Step 4: Start the Virtual Machine
Once you know the name of the virtual machine, you can start it from the command line using the following command:
Start-VM -Name <VirtualMachineName>
Replace <VirtualMachineName> with the name of the virtual machine that you want to start. For example, if the name of the virtual machine is “TestVM”, the command would be:
Start-VM -Name TestVM
This will start the virtual machine.
Step 5: Verify that the Virtual Machine has Started
To verify that the virtual machine has started, you can use the following command:
Get-VM -Name <VirtualMachineName> | Select-Object -ExpandProperty State
Replace <VirtualMachineName> with the name of the virtual machine that you started. This command will display the state of the virtual machine. If the virtual machine is running, the state will be “Running”.
Alternative Method: Using PowerShell Script
If you need to start a virtual machine from the command line frequently, you can create a PowerShell script to automate the process. Here’s how:
- Open Notepad or any text editor.
- Copy and paste the following code:
$vmName = "<VirtualMachineName>"
$vm = Get-VM -Name $vmName
Start-VM -VM $vm
- Replace <VirtualMachineName> with the name of the virtual machine that you want to start.
- Save the file with a .ps1 extension, for example, “StartVM.ps1”.
To run the script, open Command Prompt as an administrator, navigate to the directory where the script is saved, and type the following command:
powershell -ExecutionPolicy RemoteSigned -File .\StartVM.ps1
This will start the virtual machine specified in the script.
Here are a few examples of PowerShell commands that can be used to manage virtual machines:
Stopping a Virtual Machine
To stop a running virtual machine, use the following command:
Stop-VM -Name <VirtualMachineName>
This will stop the virtual machine with the specified name.
Restarting a Virtual Machine
To restart a virtual machine, use the following command:
Restart-VM -Name <VirtualMachineName>
This will stop and then start the virtual machine with the specified name.
Changing Virtual Machine Settings
To change the settings of a virtual machine, use the following command:
Set-VM -Name <VirtualMachineName> -MemoryStartupBytes <MemorySize> -ProcessorCount <NumberOfProcessors>
Replace <VirtualMachineName> with the name of the virtual machine that you want to change the settings for. Replace <MemorySize> with the desired amount of memory for the virtual machine in bytes (for example, 1 GB = 1073741824 bytes). Replace <NumberOfProcessors> with the desired number of virtual processors for the virtual machine.
Creating a New Virtual Machine
To create a new virtual machine, use the following command:
New-VM -Name <VirtualMachineName> -MemoryStartupBytes <MemorySize> -NewVHDPath <VHDPath> -NewVHDSizeBytes <VHDSize> -SwitchName <NetworkSwitchName>
Replace <VirtualMachineName> with the desired name for the new virtual machine. Replace <MemorySize> with the desired amount of memory for the virtual machine in bytes. Replace <VHDPath> with the path where the virtual hard disk (VHD) for the new virtual machine should be created. Replace <VHDSize> with the desired size of the VHD in bytes. Replace <NetworkSwitchName> with the name of the virtual switch that the virtual machine should use to connect to the network.
How to enable Hyper-V in Windows 10 using command line?
- Open an elevated Command Prompt window by right-clicking the Start menu and selecting “Command Prompt (Admin)”.
- Type the following command and press Enter to enable the Hyper-V feature:
DISM /Online /Enable-Feature /All /FeatureName:Microsoft-Hyper-V
- Restart your computer to complete the installation of the Hyper-V feature.
- Once your computer has restarted, open the Hyper-V Manager to create and manage virtual machines.
Alternatively, you can also use PowerShell to enable the Hyper-V feature. Here are the steps to follow:
- Open an elevated PowerShell window by right-clicking the Start menu and selecting “Windows PowerShell (Admin)”.
- Type the following command and press Enter to enable the Hyper-V feature:
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All
- Restart your computer to complete the installation of the Hyper-V feature.
- Once your computer has restarted, open the Hyper-V Manager to create and manage virtual machines.
Enabling Hyper-V using the command line is a quick and easy way to get started with virtualization on Windows 10. Whether you are a developer, IT professional, or just someone who wants to experiment with virtual machines, Hyper-V is a powerful tool that can help you get the job done.
Conclusion
Using PowerShell commands to manage Hyper-V virtual machines provides a fast and efficient way to perform management tasks without having to use the graphical user interface. Starting, stopping, and restarting virtual machines, changing virtual machine settings, and creating new virtual machines can all be accomplished using PowerShell commands. By learning to use these commands, Hyper-V administrators can save time and streamline virtual machine management on Windows servers. Starting a Hyper-V virtual machine from the command line is a quick and easy way to manage virtual machines on Windows servers. By following the steps outlined in this article, you can start a virtual machine from the command line and automate the process using a PowerShell script.