r/PowerShell • u/lucky789741 • May 16 '26
Script Sharing Download Nvidia Drvier based on device id just like NVIDIA APP so you don't need NVIDIA APP
function Get-NvidiaDriverUrl {
param([bool]$Laptop = $false, [bool]$Studio = $false)
$vid = '10DE'
$devs = Get-CimInstance Win32_VideoController -Filter "PNPDeviceID like '%$vid%'" |
Select-Object -ExpandProperty PNPDeviceID |
ForEach-Object { Get-PnpDevice -PresentOnly -DeviceId $_ }
if (!$devs) { $devs = Get-PnpDevice -PresentOnly -DeviceId "*$vid*" }
$gpus = @($devs | Where-Object { $_.Class -eq 'Display' -or $_.Class -eq '3D Video Controller' })
if (!$gpus) { $gpus = @($devs) }
$ids = @($gpus | ForEach-Object {
if ($_.DeviceID -match 'DEV_(\w{4}).*SUBSYS_(\w{4})(\w{4})') {
if ($Laptop) { "$($Matches[1])_$vid" }
else { "$($Matches[1])_$vid_$($Matches[2])_$($Matches[3])" }
}
})
if (!$ids) { return $null }
$v = [System.Environment]::OSVersion.Version
$body = [ordered]@{
dIDa = $ids
osC = "$($v.Major).$($v.Minor)"
osB = "$($v.Build)"
is6 = ('0','1')[[System.Environment]::Is64BitOperatingSystem]
lg = "$((Get-WinSystemLocale).LCID)"
iLp = ('0','1')[$Laptop]
prvMd = '0'
upCRD = ('0','1')[$Studio]
} | ConvertTo-Json -Compress
$body = [regex]::Unescape($body)
$r1 = Invoke-WebRequest -UseBasicParsing -ErrorAction Stop -Uri (
'https://gfwsl.geforce.com/nvidia_web_services/controller.gfeclientcontent.NG.php/' +
"com.nvidia.services.GFEClientContent_NG.getDispDrvrByDevid/$([uri]::EscapeDataString($body))"
)
if ($r1.StatusCode -ne 200) { return $null }
return (ConvertFrom-Json $r1.Content).DriverAttributes.DownloadURLAdmin
}
This returns the url of latest driver supported by your installed card, and may even return a Windows 7 driver if you run this on a Windows 7 machine with old card installed on it.
39
Upvotes
3
u/JeremyLC May 23 '26
I saw this a few days ago and wanted to comment, but did not have time. First, this is really cool. It's useful. Second, there are a few things going on here that can be improved. Starting from top to bottom
It is more idiomatically PowerShell to use a switch rather than boolean parameter. This can be done easily and they are treated the same in your code, but handled better by PowerShell.
You're making a lot of redundant queries to CIM / WMI at the top. That can be simplified. It's not a huge concern for performance, but you may as well simplify it. It becomes simpler to understand and maintain.
Next, you're using a regular expression to parse a string into pieces. My general philosophy is to only use a RgEx when it cannot be avoided. Primarily because they're often inscrutable and can present problems in the future when you come back to your code and have to remember how it works. Fortunately PowerShell has access to .Net's string parsing, which is more than adequate for this task.
You're using a single letter variable name for the OS Version. Single letter variable names can be okay, in certain contexts (like this), but it's not a good habit. Variable names should generally be long enough to tell what that variable contains.
You're doing lots of "clever" type casting. This can work, but it's less confusing to just use the built-in language features. In general, don't try to be "clever" because you'll come back to it a week later and not understand what's going on. This is first-hand experience talking.
You already addressed the manually constructed JSON, but it looks like you left behind the unneeded
[regex]::unescape(). You can use[System.Web.HttpUtiliity]::UrlEncode()to encode the JSON payload into the appropriate format for the URL. The System.Web assembly must be loaded for this.For the actual request
Invoke-RestMethodwill work nicely and will automatically parse the JSON payload, if there is one.Lastly, just because I learned to code decades ago, I also like to have only ONE return from a function, but I acknowledge that it isn't necessary in PowerShell, so I'm not telling you to code that way. I'm just explaining why I did it below.
Combining everything together, you get this
1
u/BlackV May 16 '26
I need to look at this on a computer screen, this seems slightly messy or something
Why a
booland not aswitch?Why not auto detect those anyway? What's happening with
$dev?2
u/AxelLucro May 19 '26
Great script it is slick to download the driver needed based on device id.
Consider a few things:
Where and when will this script execute? Will it run at a time when the network will be connected to the internet?
How can you be sure the network is up, the internet is accessable, and the Nvidia site is reachable before attempting to download?
If one of those items fails how will you determine which one?
What happens when the computer cannot be configured due to a script error?
Consider some volitilty isolation. Maybe gather your device ids and separate the download functionality into a separate script and run it to place the files in a more reliable storage location using the unique id as a directory name. Then on the client system grab the id dynamically and pull from reliable storage using the id as a directory/unique identifier.
You have all the pieces, but maybe split the tasks up a bit to make it fit your workflow.
5
u/PhysicalPinkOrchid May 16 '26
Manually constructing JSON is a no-no.
2
u/[deleted] May 16 '26
[removed] — view removed comment