Skip to content

Rename-HyperVSnapshot

Hyper-V: Renames 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,

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

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

        $renamed = Rename-VMSnapshot -VMSnapshot $snapshot -NewName $NewName -Passthru -ErrorAction Stop

        $result = [PSCustomObject]@{
            VMName      = $VMName
            OldName     = $Name
            NewName     = $renamed.Name
            Action      = "SnapshotRenamed"
            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 current name of the snapshot (checkpoint).

Specifies the new name for the snapshot.

An interactive directory of PowerShell scripts.