Skip to content

Set-HyperVHostConfig

Hyper-V: Configures settings for a Hyper-V host

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

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

    [PSCredential]$Credential,

    [string]$VirtualHardDiskPath,

    [string]$VirtualMachinePath,

    [bool]$NumaSpanningEnabled,

    [int]$MaximumStorageMigrations,

    [bool]$EnableEnhancedSessionMode
)

Process {
    try {
        $params = @{
            'ComputerName' = $ComputerName
            'Confirm'      = $false
            'ErrorAction'  = 'Stop'
        }
        if ($Credential) { $params.Add('Credential', $Credential) }
        
        if ($PSBoundParameters.ContainsKey('VirtualHardDiskPath')) { $params.Add('VirtualHardDiskPath', $VirtualHardDiskPath) }
        if ($PSBoundParameters.ContainsKey('VirtualMachinePath')) { $params.Add('VirtualMachinePath', $VirtualMachinePath) }
        if ($PSBoundParameters.ContainsKey('NumaSpanningEnabled')) { $params.Add('NumaSpanningEnabled', $NumaSpanningEnabled) }
        if ($PSBoundParameters.ContainsKey('MaximumStorageMigrations')) { $params.Add('MaximumStorageMigrations', $MaximumStorageMigrations) }
        if ($PSBoundParameters.ContainsKey('EnableEnhancedSessionMode')) { $params.Add('EnableEnhancedSessionMode', $EnableEnhancedSessionMode) }

        if ($params.Count -gt 3) {
            Set-VMHost @params
        }

        $hostInfo = Get-VMHost -ComputerName $ComputerName -ErrorAction Stop
        
        $result = [PSCustomObject]@{
            ComputerName              = $hostInfo.Name
            VirtualHardDiskPath       = $hostInfo.VirtualHardDiskPath
            VirtualMachinePath        = $hostInfo.VirtualMachinePath
            NumaSpanningEnabled       = $hostInfo.NumaSpanningEnabled
            MaximumStorageMigrations  = $hostInfo.MaximumStorageMigrations
            Action                    = "HostConfigUpdated"
            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.

Optional. Specifies the default folder to store virtual hard disks.

Optional. Specifies the default folder to store virtual machine configuration files.

Optional. Specifies whether virtual machines can use resources from more than one NUMA node.

Optional. Specifies the maximum number of simultaneous storage migrations.

Optional. Specifies whether Enhanced Session Mode is allowed.

An interactive directory of PowerShell scripts.