-
Notifications
You must be signed in to change notification settings - Fork 7
/
Download_NVidia_Driver.ps1
252 lines (221 loc) · 8.27 KB
/
Download_NVidia_Driver.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<#
.SYNOPSIS
Check for the latest NVIdia driver version, and if it's lower than the current one download
.PARAMETER Clean
Delete the old driver, reset settings and install the newest one
.EXAMPLE
UpdateNVidiaDriver
.NOTES
Supports Windows 10 x64 & Windows 11 only
.EXAMPLE
UpdateNVidiaDriver -Clean
#>
function UpdateNVidiaDriver
{
param
(
[Parameter(
Mandatory = $false,
ParameterSetName = "Clean"
)]
[switch]
$Clean
)
Clear-Host
# Checking Windows version
if ([System.Version][Environment]::OSVersion.Version.ToString() -lt [System.Version]"10.0")
{
Write-Verbose -Message "Your Windows is unsupported. Upgrade to Windows 10 or higher" -Verbose
exit
}
# Checking Windows bitness
if (-not [Environment]::Is64BitOperatingSystem)
{
Write-Verbose -Message "Your Windows architecture is x86. x64 is required" -Verbose
exit
}
if (Test-Path -Path "$env:SystemRoot\System32\DriverStore\FileRepository\nv_*\nvidia-smi.exe")
{
# The NVIDIA System Management Interface (nvidia-smi) is a command line utility, based on top of the NVIDIA Management Library (NVML)
$CurrentDriverVersion = nvidia-smi.exe --format=csv,noheader --query-gpu=driver_version
}
else
{
[System.Version]$Driver = (Get-CimInstance -ClassName Win32_VideoController | Where-Object -FilterScript {$_.Name -match "NVIDIA"}).DriverVersion
$CurrentDriverVersion = ("{0}{1}" -f $Driver.Build, $Driver.Revision).Substring(1).Insert(3,'.')
}
Write-Verbose -Message "Current version: $CurrentDriverVersion" -Verbose
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
if ($Host.Version.Major -eq 5)
{
# Progress bar can significantly impact cmdlet performance
# https://github.com/PowerShell/PowerShell/issues/2138
$Script:ProgressPreference = "SilentlyContinue"
}
# Checking latest driver version from Nvidia website
$Parameters = @{
Uri = "https://www.nvidia.com/Download/API/lookupValueSearch.aspx?TypeID=3"
UseBasicParsing = $true
}
[xml]$Content = (Invoke-WebRequest @Parameters).Content
$CardModelName = (Get-CimInstance -ClassName CIM_VideoController | Where-Object -FilterScript {($_.AdapterDACType -notmatch "Internal") -and ($_.Status -eq "OK")}).Caption.Split(" ")
if (-not $CardModelName)
{
Write-Verbose -Message "There's no active videocard in system" -Verbose
exit
}
# Remove the first word in full model name. E.g. "NVIDIA"
$CardModelName = [string]$CardModelName[1..($CardModelName.Count)]
$ParentID = ($Content.LookupValueSearch.LookupValues.LookupValue | Where-Object -FilterScript {$_.Name -contains $CardModelName}).ParentID | Select-Object -First 1
$Value = ($Content.LookupValueSearch.LookupValues.LookupValue | Where-Object -FilterScript {$_.Name -contains $CardModelName}).Value | Select-Object -First 1
# https://github.com/fyr77/EnvyUpdate/wiki/Nvidia-API
# osID=57 — Windows x64/Windows 11
# languageCode=1033 — English language
# dch=1 — DCH drivers
# https://nvidia.custhelp.com/app/answers/detail/a_id/4777/~/nvidia-dch%2Fstandard-display-drivers-for-windows-10-faq
# upCRD=0 — Game Ready Driver
$Parameters = @{
Uri = "https://gfwsl.geforce.com/services_toolkit/services/com/nvidia/services/AjaxDriverService.php?func=DriverManualLookup&psid=$ParentID&pfid=$Value&osID=57&languageCode=1033&beta=null&isWHQL=1&dltype=-1&dch=1&upCRD=0"
UseBasicParsing = $true
}
$Data = Invoke-RestMethod @Parameters
if ($Data.IDS.downloadInfo.Version)
{
$LatestVersion = $Data.IDS.downloadInfo.Version
Write-Verbose -Message "Latest version: $LatestVersion" -Verbose
}
else
{
Write-Warning -Message "Something went wrong"
exit
}
# Comparing installed driver version to latest driver version from Nvidia
if (-not $Clean -and ([System.Version]$LatestVersion -eq [System.Version]$CurrentDriverVersion))
{
Write-Verbose -Message "The current installed NVidia driver is the same as the latest one" -Verbose
exit
}
$DownloadsFolder = Get-ItemPropertyValue -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" -Name "{374DE290-123F-4565-9164-39C4925E467B}"
if (-not (Test-Path -Path "$DownloadsFolder\$LatestVersion-desktop-win10-win11-64bit-international-dch-whql.exe"))
{
# Downloading installer
try
{
$Parameters = @{
Uri = $Data.IDS.downloadInfo.DownloadURL
OutFile = "$DownloadsFolder\$LatestVersion-desktop-win10-win11-64bit-international-dch-whql.exe"
UseBasicParsing = $true
Verbose = $true
}
Invoke-WebRequest @Parameters
}
catch [System.Net.WebException]
{
Write-Warning -Message "Connection cannot be established"
exit
}
}
Write-Warning -Message "Downloading..."
Write-Warning -Message $Data.IDS.downloadInfo.DownloadURL
# Get the latest 7-Zip download URL
try
{
$Parameters = @{
Uri = "https://sourceforge.net/projects/sevenzip/best_release.json"
UseBasicParsing = $true
Verbose = $true
}
$bestRelease = (Invoke-RestMethod @Parameters).platform_releases.windows.filename.replace("exe", "msi")
}
catch [System.Net.WebException]
{
Write-Warning -Message "Connection cannot be established"
exit
}
# Download the latest 7-Zip x64
try
{
$Parameters = @{
Uri = "https://unlimited.dl.sourceforge.net/project/sevenzip$($bestRelease)?viasf=1"
OutFile = "$DownloadsFolder\7Zip.msi"
UseBasicParsing = $true
Verbose = $true
}
Invoke-WebRequest @Parameters
}
catch [System.Net.WebException]
{
Write-Warning -Message "Connection cannot be established"
exit
}
# Expand 7-Zip
$Arguments = @(
"/a `"$DownloadsFolder\7Zip.msi`""
"TARGETDIR=`"$DownloadsFolder\7zip`""
"/qb"
)
Start-Process "msiexec" -ArgumentList $Arguments -Wait
# Delete the installer once it completes
Remove-Item -Path "$DownloadsFolder\7Zip.msi" -Force
# Extracting installer
# Based on 7-zip.chm
$Arguments = @(
# Extracts files from an archive with their full paths in the current directory, or in an output directory if specified
"x",
# standard output messages. disable stream
"-bso0",
# progress information. redirect to stdout stream
"-bsp1",
# error messages. redirect to stdout stream
"-bse1",
# Overwrite All existing files without prompt
"-aoa",
# What to extract
"$DownloadsFolder\$LatestVersion-desktop-win10-win11-64bit-international-dch-whql.exe",
# Extract these files and folders
"Display.Driver HDAudio NVI2 PhysX EULA.txt ListDevices.txt setup.cfg setup.exe",
# Specifies a destination directory where files are to be extracted
"-o`"$DownloadsFolder\NVidia`""
)
$Parameters = @{
FilePath = "$DownloadsFolder\7zip\Files\7-Zip\7z.exe"
ArgumentList = $Arguments
NoNewWindow = $true
Wait = $true
}
Start-Process @Parameters
# Remove unneeded dependencies from setup.cfg
[xml]$Config = Get-Content -Path "$DownloadsFolder\NVidia\setup.cfg" -Encoding UTF8 -Force
$Node = $Config.SelectSingleNode("//file[@name='`${{EulaHtmlFile}}']")
$Node.ParentNode.RemoveChild($Node)
$Node = $Config.SelectSingleNode("//file[@name='`${{FunctionalConsentFile}}']")
$Node.ParentNode.RemoveChild($Node)
$Node = $Config.SelectSingleNode("//file[@name='`${{PrivacyPolicyFile}}']")
$Node.ParentNode.RemoveChild($Node)
$Config.Save("$DownloadsFolder\NVidia\setup.cfg")
# Re-save in the UTF-8 without BOM encoding to make it work
Set-Content -Value (New-Object -TypeName System.Text.UTF8Encoding -ArgumentList $false).GetBytes($(Get-Content -Path "$DownloadsFolder\NVidia\setup.cfg" -Raw)) -Encoding Byte -Path "$DownloadsFolder\NVidia\setup.cfg" -Force
if ($Clean)
{
# Clean installation
$Arguments = @("-passive", "-noreboot", "-noeula", "-nofinish", "-clean")
}
else
{
$Arguments = @("-passive", "-noreboot", "-noeula", "-nofinish")
}
# Create a batch file
$Setupcmd = @"
`"%~dp0setup.exe`" $($Arguments)
"@
Set-Content -Path "$DownloadsFolder\NVidia\setup.cmd" -Value $Setupcmd -Encoding Default -Force
$Parameters = @{
Path = "$DownloadsFolder\7zip", "$DownloadsFolder\$LatestVersion-desktop-win10-win11-64bit-international-dch-whql.exe"
Recurse = $true
Force = $true
}
Remove-Item @Parameters
Invoke-Item -Path "$DownloadsFolder\NVidia"
Write-Warning -Message "Run `"$DownloadsFolder\NVidia\setup.cmd`" as administrator to install downloaded NVidia driver"
}
UpdateNVidiaDriver -Clean