OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

(RESOLVED) How can I use PowerShell to determine the active graphics card with the highest VRAM capacity?

  • Thread starter Thread starter SkayDev
  • Start date Start date
S

SkayDev

Guest
Okay, I'm not one to ask for help, but I've been struggling for days with a feature I want to implement in a private OpenAI Whisper project. To put it in context, I'm doing a project to automate everything, including AI model selection based on computer performance. I'm doing this only in a powershell language.

The aim is to get the best graphics card on the computer, get its VRAM in GB and tell my script ok, take this model.

The problem I'm having is that my script does detect the name of the selected graphics card, but it keeps the best value it finds in the registry editor after tests to change the graphics card... etc... I'm not a code expert, so if it does find a solution, it'll be a simple one or maybe not.

I'd like to point out that the script must also include whether or not the processor plays the role of the graphics card if there is none.

I tried to get the active grahpics cards on the computer by looking in Win32_VideoController then, I have the impression that you can't find the VRAM value with a Get-CimInstance so I thought I'd go to the registry editor to get the HardwareInformation.qwMemorySize key and then do a GB conversion. Then, to find out if the card is active and avoid it taking the value from me, I wanted to make a comparison between the "Name" value of the Win32_VideoController and the data in the DriverDesc register, except that apparently the data doesn't match, which gives me an error and it doesn't find anything.

Here's my current code ;

Code:
# Function to convert VRAM size to GB
function ConvertTo-GB {
    param (
        [double]$bytes
    )
    return [math]::round($bytes / 1GB)
}

# Recover all graphics cards except Microsoft Remote Display Adapter
$gpus = Get-CimInstance -ClassName Win32_VideoController | Where-Object {
    $_.Name -notmatch "Microsoft Remote Display Adapter" -and $_.Status -eq "OK"
}

# Initialize variables
$maxVRAM = 0
$selectedGPU = $null

# Retrieve HardwareInformation.qwMemorySize and DriverDesc values from the registry
$keyPath = "HKLM:\SYSTEM\ControlSet001\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}\0*"
$registryValues = Get-ItemProperty -Path $keyPath -Name HardwareInformation.qwMemorySize,DriverDesc -ErrorAction SilentlyContinue

# Browse all graphics cards to find the one with the largest VRAM
foreach ($gpu in $gpus) {
    # Check that the DriverDesc property is set
    if ($gpu.DriverDesc -ne $null) {
        # Find the corresponding value of HardwareInformation.qwMemorySize
        $registryValue = $registryValues | Where-Object { $_.DriverDesc.Trim().ToLower() -eq $gpu.DriverDesc.Trim().ToLower() }
        if ($registryValue -ne $null) {
            $qwMemorySize = $registryValue.HardwareInformation.qwMemorySize[0]
            if ($qwMemorySize -ne $null) {
                $vram = ConvertTo-GB -bytes $qwMemorySize
                Write-Host "Found VRAM for $($gpu.Name): $vram GB"
            } else {
                Write-Warning "Unable to retrieve VRAM for $($gpu.Name) from registry key: $keyPath"
                $vram = 0
            }
        } else {
            Write-Warning "Unable to find registry value for $($gpu.Name) with DriverDesc: $($gpu.DriverDesc)"
            $vram = 0
        }
    } else {
        Write-Warning "DriverDesc is not defined for $($gpu.Name)"
        $vram = 0
    }

    # Check if this board has the largest VRAM found
    if ($vram -gt $maxVRAM) {
        $maxVRAM = $vram
        $selectedGPU = $gpu
    }
}

# Check if a graphics card has been selected
if ($selectedGPU -eq $null) {
    Write-Warning "No suitable GPU found."
    $model = "none"
    $selectedGPUName = "None"
} else {
    # Select the appropriate model according to VRAM quantity
    switch ($maxVRAM) {
        { $_ -lt 2 } { $model = "base" }
        { $_ -lt 5 } { $model = "small" }
        { $_ -lt 10 } { $model = "medium" }
        default { $model = "large-v3" }
    }
    $selectedGPUName = $selectedGPU.Caption
}

# Display selected model and VRAM quantity
$result = [PSCustomObject] @{
    Model = $selectedGPUName
    "VRAM, GB" = $maxVRAM
    "Selected Model" = $model
}

$result | Format-Table -AutoSize

# Use the selected template for your Whisper project
Write-Host "Model $model selected for your Whisper project with $maxVRAM Go of VRAM"

Is there perhaps another solution? Using a separate module or possibly a program that I'll put in my final installation package? Anyway, thanks in advance for any help you can give me.
<p>Okay, I'm not one to ask for help, but I've been struggling for days with a feature I want to implement in a private OpenAI Whisper project. To put it in context, I'm doing a project to automate everything, including AI model selection based on computer performance. I'm doing this only in a powershell language.</p>
<p>The aim is to get the best graphics card on the computer, get its VRAM in GB and tell my script ok, take this model.</p>
<p>The problem I'm having is that my script does detect the name of the selected graphics card, but it keeps the best value it finds in the registry editor after tests to change the graphics card... etc... I'm not a code expert, so if it does find a solution, it'll be a simple one or maybe not.</p>
<p>I'd like to point out that the script must also include whether or not the processor plays the role of the graphics card if there is none.</p>
<p>I tried to get the active grahpics cards on the computer by looking in <code>Win32_VideoController</code> then, I have the impression that you can't find the VRAM value with a <code>Get-CimInstance</code> so I thought I'd go to the registry editor to get the <code>HardwareInformation.qwMemorySize</code> key and then do a GB conversion. Then, to find out if the card is active and avoid it taking the value from me, I wanted to make a comparison between the "Name" value of the <code>Win32_VideoController</code> and the data in the <code>DriverDesc</code> register, except that apparently the data doesn't match, which gives me an error and it doesn't find anything.</p>
<p>Here's my current code ;</p>
<pre><code># Function to convert VRAM size to GB
function ConvertTo-GB {
param (
[double]$bytes
)
return [math]::round($bytes / 1GB)
}

# Recover all graphics cards except Microsoft Remote Display Adapter
$gpus = Get-CimInstance -ClassName Win32_VideoController | Where-Object {
$_.Name -notmatch "Microsoft Remote Display Adapter" -and $_.Status -eq "OK"
}

# Initialize variables
$maxVRAM = 0
$selectedGPU = $null

# Retrieve HardwareInformation.qwMemorySize and DriverDesc values from the registry
$keyPath = "HKLM:\SYSTEM\ControlSet001\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}\0*"
$registryValues = Get-ItemProperty -Path $keyPath -Name HardwareInformation.qwMemorySize,DriverDesc -ErrorAction SilentlyContinue

# Browse all graphics cards to find the one with the largest VRAM
foreach ($gpu in $gpus) {
# Check that the DriverDesc property is set
if ($gpu.DriverDesc -ne $null) {
# Find the corresponding value of HardwareInformation.qwMemorySize
$registryValue = $registryValues | Where-Object { $_.DriverDesc.Trim().ToLower() -eq $gpu.DriverDesc.Trim().ToLower() }
if ($registryValue -ne $null) {
$qwMemorySize = $registryValue.HardwareInformation.qwMemorySize[0]
if ($qwMemorySize -ne $null) {
$vram = ConvertTo-GB -bytes $qwMemorySize
Write-Host "Found VRAM for $($gpu.Name): $vram GB"
} else {
Write-Warning "Unable to retrieve VRAM for $($gpu.Name) from registry key: $keyPath"
$vram = 0
}
} else {
Write-Warning "Unable to find registry value for $($gpu.Name) with DriverDesc: $($gpu.DriverDesc)"
$vram = 0
}
} else {
Write-Warning "DriverDesc is not defined for $($gpu.Name)"
$vram = 0
}

# Check if this board has the largest VRAM found
if ($vram -gt $maxVRAM) {
$maxVRAM = $vram
$selectedGPU = $gpu
}
}

# Check if a graphics card has been selected
if ($selectedGPU -eq $null) {
Write-Warning "No suitable GPU found."
$model = "none"
$selectedGPUName = "None"
} else {
# Select the appropriate model according to VRAM quantity
switch ($maxVRAM) {
{ $_ -lt 2 } { $model = "base" }
{ $_ -lt 5 } { $model = "small" }
{ $_ -lt 10 } { $model = "medium" }
default { $model = "large-v3" }
}
$selectedGPUName = $selectedGPU.Caption
}

# Display selected model and VRAM quantity
$result = [PSCustomObject] @{
Model = $selectedGPUName
"VRAM, GB" = $maxVRAM
"Selected Model" = $model
}

$result | Format-Table -AutoSize

# Use the selected template for your Whisper project
Write-Host "Model $model selected for your Whisper project with $maxVRAM Go of VRAM"
</code></pre>
<p>Is there perhaps another solution? Using a separate module or possibly a program that I'll put in my final installation package? Anyway, thanks in advance for any help you can give me.</p>
Continue reading...
 

Latest posts

Top