Private/Update-SpecComPortSettings.ps1

function Update-SpecComPortSettings {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$PortKey,

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

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

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

    $reg = $false

    try {
        Set-ItemProperty -Path $PortKey -Name "PortName" -Value $PortNumberString -ErrorAction Stop
        Write-Host "PortName set to '$PortNumberString'" -ForegroundColor DarkGray
        $reg = $true
    } catch {
        Write-Error "Failed to update Portname: $_"
        return 101
    }

    if ($reg) {
        try {
            Set-ItemProperty -Path $DeviceKey -Name "FriendlyName" -Value "$ComPortDescription ($PortNumberString)" -ErrorAction Stop
            Write-Host "FriendlyName set to '$($ComPort.Description) ($PortNumberString)'" -ForegroundColor DarkGray
        } catch {
            Write-Error "Failed to update the FriendlyName property: $_"
            return 102
        }
    }
}