Create Shortcut of Hyper-V Virtual Machine in Windows  

Page 2 of 3 FirstFirst 123 LastLast

  1. Posts : 27,182
    Win11 Pro, Win10 Pro N, Win10 Home, Windows 8.1 Pro, Ubuntu
       #10

    @Kari here are two CentOS icons I made for you:
    2 CentOS icons.zip

    Here are the PNG versions to show what they look like.
    Create Shortcut of Hyper-V Virtual Machine in Windows-centos-vm.png.png Create Shortcut of Hyper-V Virtual Machine in Windows-centos-alternative.png.png
      My Computers


  2. Posts : 17,661
    Windows 10 Pro
    Thread Starter
       #11

    Thanks! I like them, especially the blue one.
      My Computer


  3. Posts : 27,182
    Win11 Pro, Win10 Pro N, Win10 Home, Windows 8.1 Pro, Ubuntu
       #12

    You're welcome, the blue came out better, because the other was originally a gif with transparency, but after changing it to PNG then ICO it lost the transparency and got a white background.
      My Computers


  4. Posts : 4,802
    Windows 10 ProWorkstation -- Whichever is highest build number.
       #13

    I know this is an old thread, but I think the original intent was forgotten. That is to start a Hyper-V virtual machine from a shortcut. I put all of these files I am about to relate into a folder called HyperV inside a folder on my C-drive called Customizations where I have various other stuff stored. So I have C:\Customizations\HyperV. My first file is StartVM.ps1 it has this code in it. My Hyper-V machine, I might add, is called test.
    Code:
    param([switch]$Elevated)
    
    function Test-Admin {
      $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
      $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
    }
    
    
    if ((Test-Admin) -eq $false)  {
        if ($elevated) 
        {
            # tried to elevate, did not work, aborting
        } 
        else {
            Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
    }
    
    
    exit
    }
    
    
    'running with full privileges'
    
    
    $t = '[DllImport("user32.dll")] public static extern bool ShowWindow(int handle, int state);'
    add-type -name win -member $t -namespace native
    [native.win]::ShowWindow(([System.Diagnostics.Process]::GetCurrentProcess() | Get-Process).MainWindowHandle, 0)
    
    
    Start-VM "test"
    cmd /c %SYSTEMROOT%\System32\vmconnect.exe localhost "test" | Out-Null
    Stop-VM "test"
    
    
    Stop-Process -processname powershell
    My next file is StartVM.cmd, and it contains this
    Code:
    @REM [This code block detects if the script is running with ADMIN PRIVILEGES. If it isn't it pauses, and then quits.]echo OFFcls
    NET SESSION >nul 2>&1
    IF %ERRORLEVEL% EQU 0 (
        ECHO Administrator PRIVILEGES Detected! 
    ) ELSE (
       echo ######## ########  ########   #######  ########  
       echo ##       ##     ## ##     ## ##     ## ##     ## 
       echo ##       ##     ## ##     ## ##     ## ##     ## 
       echo ######   ########  ########  ##     ## ########  
       echo ##       ##   ##   ##   ##   ##     ## ##   ##   
       echo ##       ##    ##  ##    ##  ##     ## ##    ##  
       echo ######## ##     ## ##     ##  #######  ##     ## 
       echo.
       echo.
       echo ####### ERROR: ADMINISTRATOR PRIVILEGES REQUIRED #########
       echo This script must be run as administrator to work properly!  
       echo If you are seeing this right click on All_In_One.CMD and select "Run As Administrator."
       echo ##########################################################
       echo.
       PAUSE
       EXIT /B 1
    )
    @echo ON
    
    
    
    
    %~d0
    cd %~d0%~p0
    cls
    powershell "& "".\StartVM.ps1"""
    My third file is for aesthetics so I will not see any command windows running in the background. It is VirtualMachine.vbs, and contains this
    Code:
    command = "powershell.exe -nologo -command %SYSTEMDRIVE%\Customizations\HyperV\StartVM.cmd"set shell = CreateObject("Wscript.Shell")
    shell.Run command,0
    My fourth file is a shortcut to VirtualMachine.vbs. I named it VirtualMachine. I pinned it to the Start Menu, opened the file location, and set it to run as administrator. That is all there is to it. Be sure to change the name of the virtual machine in the code from my "test" to whatever you have named yours!
      My Computer


  5. Posts : 4
    Windows 10
       #14

    flybynite said:
    I know this is an old thread, but I think the original intent was forgotten. That is to start a Hyper-V virtual machine from a shortcut. I put all of these files I am about to relate into a folder called HyperV inside a folder on my C-drive called Customizations where I have various other stuff stored. So I have C:\Customizations\HyperV. My first file is StartVM.ps1 it has this code in it. My Hyper-V machine, I might add, is called test.

    [CODE]

    My fourth file is a shortcut to VirtualMachine.vbs. I named it VirtualMachine. I pinned it to the Start Menu, opened the file location, and set it to run as administrator. That is all there is to it. Be sure to change the name of the virtual machine in the code from my "test" to whatever you have named yours!
    Thanks for this. It was just what I was looking for. But note that it has some errors due to missing line breaks.

    In the second file (.cmd), the first line should be broken into three lines:
    @Rem [This code block detects if the script is running with ADMIN PRIVILEGES. If it isn't it pauses, and then quits.]
    echo OFF
    cls

    And in the third file (.vbs), the first line should be broken into two lines:
    command = "powershell.exe -nologo -command %SYSTEMDRIVE%\Users\Steve\Desktop\VMLaunch\StartVM.cmd"
    set shell = CreateObject("Wscript.Shell")

    Also note that Windows would not let me elevate the vbs file or its shortcut, to Run As Admin. This was solved by setting the shortcut to point to the windows script host executable, making it's target:
    C:\Windows\System32\wscript.exe "C:\[file directory path]\VirtualMachine.vbs"

    I also ran into a problem with powershell scripts being restricted from running on my computer (this is the default Windows setting). I overcame this by adding the Set-ExecutionPolicy command to the .cmd file. I also decided to set the policy back to restricted after the VM was closed, but this is optional. So I replaced the last line of the .cmd file with the following three lines (last line optional):
    powershell Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
    powershell "& "".\StartVM.ps1"""
    powershell Set-ExecutionPolicy -ExecutionPolicy Undefined

    One other note, if you want to control how the VM exits, just remove the Stop-VM "test" line from the first (.ps1) file. Then you can choose to pause, save, or shutdown before closing the connect window, or you can even just close the connect window and let the VM continue running. You can then use the shortcut to reconnect to the VM regardless of which state was chosen.

    Hope this helps others.
      My Computer


  6. Posts : 4,802
    Windows 10 ProWorkstation -- Whichever is highest build number.
       #15

    I actually have just 3 files now if you include a shortcut to the vbs file. The vbs file looks like this
    Code:
    command = "powershell.exe -nologo -command %SYSTEMDRIVE%\Customizations\HyperV\StartVM.ps1"set shell = CreateObject("Wscript.Shell")
    shell.Run command,0
    and the ps1 file is this
    Code:
    param([switch]$Elevated)
    
    
    function Test-Admin {
      $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
      $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
    }
    
    
    if ((Test-Admin) -eq $false)  {
        if ($elevated) 
        {
            # tried to elevate, did not work, aborting
        } 
        else {
            Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
    }
    
    
    exit
    }
    
    
    'running with full privileges'
    
    
    $t = '[DllImport("user32.dll")] public static extern bool ShowWindow(int handle, int state);'
    add-type -name win -member $t -namespace native
    [native.win]::ShowWindow(([System.Diagnostics.Process]::GetCurrentProcess() | Get-Process).MainWindowHandle, 0)
    
    
    Start-VM "test"
    cmd /c %SYSTEMROOT%\System32\vmconnect.exe localhost "test" | Out-Null
    Stop-VM "test"
    
    
    Stop-Process -processname powershell
    I put a shortcut to the vbs in the same folder, and check run as administrator under properties -> advanced. I pin it to Start.
      My Computer


  7. Posts : 4
    Windows 10
       #16

    flybynite said:
    I actually have just 3 files now if you include a shortcut to the vbs file. The vbs file looks like this

    ...code...
    Actually, I have found that you only need the .ps1 file. You can run it from a shortcut and get the same behavior. Just make a shortcut to the powershell executable and then go to the shortcut's properties and change the "target" field to call the .ps1 file as follows. (This also overcomes any execution policy issue. And you should also change the "Start in" field to the file directory path.)

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "C:\[file directory path]\StartVM.ps1"

    And even better, to avoid the UAC window popping up every time you run it, make it into a task and then make a shortcut to the task using the instructions here:
    http://www.sevenforums.com/tutorials...pt-create.html

    Follow the instructions, except in steps 7 and 8.

    In step 7, in the "Program" field enter the path to the powershell executable:
    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

    In step 8, in the "Add Arguments" field enter:
    -ExecutionPolicy Bypass -File "C:\[file directory path]\StartVM.ps1"
    and in the "Start in" field enter the file directory path
      My Computer


  8. Posts : 4,802
    Windows 10 ProWorkstation -- Whichever is highest build number.
       #17

    SteveFL99 said:
    Actually, I have found that you only need the .ps1 file. You can run it from a shortcut and get the same behavior. Just make a shortcut to the powershell executable and then go to the shortcut's properties and change the "target" field to call the .ps1 file as follows. (This also overcomes any execution policy issue. And you should also change the "Start in" field to the file directory path.)

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "C:\[file directory path]\StartVM.ps1"

    And even better, to avoid the UAC window popping up every time you run it, make it into a task and then make a shortcut to the task using the instructions here:
    http://www.sevenforums.com/tutorials...pt-create.html

    Follow the instructions, except in steps 7 and 8.

    In step 7, in the "Program" field enter the path to the powershell executable:
    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

    In step 8, in the "Add Arguments" field enter:
    -ExecutionPolicy Bypass -File "C:\[file directory path]\StartVM.ps1"
    and in the "Start in" field enter the file directory path
    I never have a UAC prompt appear, and I don't have any other windows appear at all. I just click the shortcut, and the Hyper-V VM starts. No additional windows popping up, no UAC prompts. Nothing.
      My Computer


  9. Posts : 4
    Windows 10
       #18

    I took the powershell code and added to it so that it will start a VM from any state and then gives you options on how you want to shut it down. You can use a shortcut (to powershell or a task, as explained above) to run the code.

    When launched, this code starts or resumes the virtual machine and then connects to it. When finished, just close the connection window and you will get a popup asking how you want to exit. You can pause (suspend), hibernate (save), or shut down (stop) the VM, or leave it running, by clicking the appropriate button or typing a letter. A short popup alerts you when that action (e.g., save or shutdown) is complete. I thought others might find this useful for managing a VM using just a shortcut.

    Code:
    
    $t = '[DllImport("user32.dll")] public static extern bool ShowWindow(int handle, int state);'
    add-type -name win -member $t -namespace native
    [native.win]::ShowWindow(([System.Diagnostics.Process]::GetCurrentProcess() | Get-Process).MainWindowHandle, 0)
    
    
    $TestVM = Get-VM "Windows 10 VM"
    $StartOnState = {
    switch -wildcard ($_.State) 
        { 
        "Pa*" {Resume-VM "Windows 10 VM"} 
            "Of*" {Start-VM "Windows 10 VM"} 
            "Save*" {Start-VM "Windows 10 VM"} 
            default {}
        }
    }
    
    $TestVM | ? $StartOnState 
    
    cmd /c %SYSTEMROOT%\System32\vmconnect.exe localhost "Windows 10 VM" | Out-Null
    
    
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
    
    $objForm = New-Object System.Windows.Forms.Form 
    $objForm.Text = "Windows 10 VM"
    $objForm.Size = New-Object System.Drawing.Size(330,152) 
    $objForm.StartPosition = "CenterScreen"
    
    $objForm.KeyPreview = $True
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
        {$script:selection=$objTextBox.Text;$objForm.Close()}})
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "R") 
        {$script:selection="R";$objForm.Close()}})
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "P") 
        {$script:selection="P";$objForm.Close()}})
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "H") 
        {$script:selection="H";$objForm.Close()}})
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "S") 
        {$script:selection="S";$objForm.Close()}})
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
        {$script:selection=0;$objForm.Close()}})
    
    $RButton = New-Object System.Windows.Forms.Button
    $RButton.Location = New-Object System.Drawing.Size(4,75)
    $RButton.Size = New-Object System.Drawing.Size(75,23)
    $RButton.Text = "Running"
    $RButton.Add_Click({$script:selection="R";$objForm.Close()})
    $objForm.Controls.Add($RButton)
    
    $PButton = New-Object System.Windows.Forms.Button
    $PButton.Location = New-Object System.Drawing.Size(81,75)
    $PButton.Size = New-Object System.Drawing.Size(75,23)
    $PButton.Text = "Pause"
    $PButton.Add_Click({$script:selection="P";$objForm.Close()})
    $objForm.Controls.Add($PButton)
    
    $HButton = New-Object System.Windows.Forms.Button
    $HButton.Location = New-Object System.Drawing.Size(158,75)
    $HButton.Size = New-Object System.Drawing.Size(75,23)
    $HButton.Text = "Hibernate"
    $HButton.Add_Click({$script:selection="H";$objForm.Close()})
    $objForm.Controls.Add($HButton)
    
    $SButton = New-Object System.Windows.Forms.Button
    $SButton.Location = New-Object System.Drawing.Size(235,75)
    $SButton.Size = New-Object System.Drawing.Size(75,23)
    $SButton.Text = "Shut down"
    $SButton.Add_Click({$script:selection="S";$objForm.Close()})
    $objForm.Controls.Add($SButton)
    
    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(7,10) 
    $objLabel.Size = New-Object System.Drawing.Size(300,30) 
    $objLabel.Text = "How to exit VM:
    [P]ause, [H]ibernate, [S]hut down, or Keep [R]unning"
    $objForm.Controls.Add($objLabel) 
    
    $objTextBox = New-Object System.Windows.Forms.TextBox 
    $objTextBox.Location = New-Object System.Drawing.Size(4,44) 
    $objTextBox.Size = New-Object System.Drawing.Size(305,20) 
    
    $objForm.Controls.Add($objTextBox) 
    
    $objForm.Topmost = $True
    
    $objForm.Add_Shown({$objForm.Activate()})
    [void] $objForm.ShowDialog()
    
    
    switch -wildcard ($script:selection) 
        { 
            "R*" {}
        "P*" {Suspend-VM "Windows 10 VM"} 
            "H*" {Save-VM "Windows 10 VM"} 
            "S*" {Stop-VM "Windows 10 VM"} 
            default {}
        }
    
    $g = new-object -comobject wscript.shell
    $b = $g.popup("Done",3,"Windows 10 VM",1088)
    
    Stop-Process -processname powershell
    
    


    Note that my virtual machine is named "Windows 10 VM". Substitute the name of your VM in the code. Also, if you make it a task, be sure to lower the prioirty of the task to 5 or 6. The default is 7, which causes it to run slow if other processes are using resources.
      My Computer


  10. Posts : 4,802
    Windows 10 ProWorkstation -- Whichever is highest build number.
       #19

    I had to put this code in it to get it to work. Without it I do not have administrator privilidges.
    Code:
    param([switch]$Elevated)function Test-Admin {  $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())  $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)}if ((Test-Admin) -eq $false)  {    if ($elevated)     {        # tried to elevate, did not work, aborting    }     else {        Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))}exit}'running with full privileges'
    Your code works with this in it. I like my code better though. There are no pop up windows, and when you turn it off, it turns off. No questions or extra steps.
      My Computer


 

Tutorial Categories

Create Shortcut of Hyper-V Virtual Machine in Windows Tutorial Index Network & Sharing Instalation and Upgrade Browsers and Email General Tips Gaming Customization Apps and Features Virtualization BSOD System Security User Accounts Hardware and Drivers Updates and Activation Backup and Restore Performance and Maintenance Mixed Reality Phone


  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 09:51.
Find Us




Windows 10 Forums