Skip to content

Get-HyperVSnapshot

Hyper-V: Lists snapshots associated with a virtual machine

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

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

    [PSCredential]$Credential,

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

    [ValidateSet('Standard', 'Recovery', 'Replica', 'AppConsistentReplica')]
    [string]$SnapshotType
)

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

        $snapshots = Get-VMSnapshot @params
        
        $results = foreach ($s in $snapshots) {
            [PSCustomObject]@{
                Name         = $s.Name
                VMName       = $s.VMName
                SnapshotType = $s.SnapshotType
                CreationTime = $s.CreationTime
                ComputerName = $s.ComputerName
            }
        }

        Write-Output ($results | Sort-Object CreationTime -Descending)
    }
    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.

Optional. Specifies the type of snapshots to retrieve (e.g., Standard, Recovery).

An interactive directory of PowerShell scripts.