Skip to content

Export-HyperVSnapshot

Hyper-V: Exports a virtual machine snapshot to disk

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

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

    [PSCredential]$Credential,

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

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

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

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

        $snapshot = Get-VMSnapshot @params

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

        Export-VMSnapshot -VMSnapshot $snapshot -Path $Path -ErrorAction Stop

        $result = [PSCustomObject]@{
            VMName       = $VMName
            SnapshotName = $SnapshotName
            ExportPath   = $Path
            Action       = "SnapshotExported"
            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 export.

Specifies the destination directory for the exported snapshot.

An interactive directory of PowerShell scripts.