Skip to content

Export-HyperVVMCheckpoint

Hyper-V: Exports a virtual machine checkpoint to a specified path

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

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

    [PSCredential]$Credential,

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

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

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

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'."
        }

        $checkpoint = Get-VMSnapshot -VM $vm -Name $CheckpointName -ErrorAction Stop

        if (-not $checkpoint) {
            throw "Checkpoint '$CheckpointName' not found for VM '$Name'."
        }

        if (-not (Test-Path -Path $Path)) {
            New-Item -ItemType Directory -Path $Path -Force | Out-Null
        }

        Export-VMSnapshot -VMSnapshot $checkpoint -Path $Path -ErrorAction Stop

        $result = [PSCustomObject]@{
            VMName         = $vm.Name
            CheckpointName = $checkpoint.Name
            ExportPath     = $Path
            Action         = "CheckpointExported"
            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 name of the checkpoint to export.

Specifies the directory path where the checkpoint will be exported.

An interactive directory of PowerShell scripts.