Skip to content

Restore-HyperVSnapshot

Hyper-V: Restores a virtual machine snapshot

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

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

    [PSCredential]$Credential,

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

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

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

        $snapshot = Get-VMSnapshot @params

        if (-not $snapshot) {
            throw "Snapshot '$Name' not found on VM '$VMName'."
        }

        Restore-VMSnapshot -VMSnapshot $snapshot -Confirm:$false -ErrorAction Stop

        $result = [PSCustomObject]@{
            VMName       = $VMName
            SnapshotName = $Name
            Action       = "SnapshotRestored"
            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 of the virtual machine.

Specifies the name of the snapshot (checkpoint) to restore.

An interactive directory of PowerShell scripts.