Skip to content

New-HyperVSnapshot

Hyper-V: Creates a new virtual machine snapshot

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

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

    [PSCredential]$Credential,

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

    [string]$Name
)

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

        $snapshot = Checkpoint-VM @params

        $result = [PSCustomObject]@{
            VMName       = $VMName
            SnapshotName = $snapshot.Name
            CreationTime = $snapshot.CreationTime
            Action       = "SnapshotCreated"
            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.

Optional. Specifies a custom name for the snapshot. If omitted, Hyper-V generates a default name.

An interactive directory of PowerShell scripts.