Skip to content

Set-HyperVVMState

Hyper-V: Manages the power state of a virtual machine

#Requires -Version 5.1
#Requires -Modules Hyper-V

[CmdletBinding()]
Param (
    [string]$ComputerName = "localhost",

    [PSCredential]$Credential,

    [Parameter(Mandatory = $true)]
    [string]$Name,

    [Parameter(Mandatory = $true)]
    [ValidateSet('Start', 'Stop', 'Suspend', 'Resume', 'Restart', 'TurnOff', 'Save')]
    [string]$Action,

    [switch]$Force
)

Process {
    try {
        $params = @{
            'ComputerName' = $ComputerName
            'ErrorAction'  = 'Stop'
        }
        if ($Credential) { $params.Add('Credential', $Credential) }

        $vm = Get-VM @params | Where-Object { $_.Name -eq $Name -or $_.Id -eq $Name }

        if (-not $vm) {
            throw "Virtual machine '$Name' not found on '$ComputerName'."
        }

        switch ($Action) {
            'Start'    { Start-VM -VM $vm }
            'Stop'     { Stop-VM -VM $vm -Force:$Force -Confirm:(-not $Force) }
            'TurnOff'  { Stop-VM -VM $vm -TurnOff }
            'Save'     { Stop-VM -VM $vm -Save }
            'Suspend'  { Suspend-VM -VM $vm }
            'Resume'   { Resume-VM -VM $vm }
            'Restart'  { Restart-VM -VM $vm -Force:$Force -Confirm:(-not $Force) }
        }

        $result = [PSCustomObject]@{
            Name      = $vm.Name
            Action    = $Action
            Status    = "Success"
            Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        }

        Write-Output $result
    }
    catch {
        throw
    }
}

Specifies the name of the Hyper-V host. Defaults to the local machine.

Specifies the credentials to use for the remote connection.

Specifies the name or ID of the virtual machine.

Specifies the power state action to perform (Start, Stop, Suspend, Resume, Restart, TurnOff, Save).

Off

If set, forces the operation (e.g., for Stop or Restart).

An interactive directory of PowerShell scripts.