Skip to content

Set-HyperVVMVideo

Hyper-V: Configures virtual machine video settings

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

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

    [PSCredential]$Credential,

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

    [Parameter(Mandatory = $true)]
    [ValidateSet('Default', 'Maximum', 'Single')]
    [string]$ResolutionType,

    [Parameter(Mandatory = $true)]
    [uint16]$HorizontalResolution,

    [Parameter(Mandatory = $true)]
    [uint16]$VerticalResolution
)

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

        Set-VMVideo -VM $vm -ResolutionType $ResolutionType -HorizontalResolution $HorizontalResolution -VerticalResolution $VerticalResolution -ErrorAction Stop

        $updatedVideo = Get-VMVideo -VM $vm
        
        $result = [PSCustomObject]@{
            VMName               = $vm.Name
            ResolutionType       = $updatedVideo.ResolutionType
            HorizontalResolution = $updatedVideo.HorizontalResolution
            VerticalResolution   = $updatedVideo.VerticalResolution
            Action               = "VideoSettingsUpdated"
            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 resolution type (Default, Maximum, Single).

Specifies the horizontal resolution in pixels.

Specifies the vertical resolution in pixels.

An interactive directory of PowerShell scripts.