-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDriverUpdate.ps1
More file actions
208 lines (178 loc) · 7.25 KB
/
Copy pathDriverUpdate.ps1
File metadata and controls
208 lines (178 loc) · 7.25 KB
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
<#
.SYNOPSIS
Updates drivers and firmware on Windows devices using native Windows methods and PSWindowsUpdate module.
GitHub Repository: https://github.com/roalhelm/PowershellScripts
.DESCRIPTION
This script automates the process of updating device drivers on Windows systems by:
1. Checking for available driver updates using Windows Update services
2. Automatically installing the PSWindowsUpdate module if not present
3. Downloading and installing driver updates with detailed logging
4. Handling BitLocker protection by temporarily suspending it during updates
5. Providing comprehensive error handling and status reporting
6. Supporting both interactive and remote execution modes
The script requires administrator privileges and logs all activities to a central log file
for auditing and troubleshooting purposes.
.NOTES
File Name : DriverUpdate.ps1
Author : Ronny Alhelm
Version : 1.1
Creation Date : October 13, 2025
Prerequisite : PowerShell 5.1 or higher, Administrator rights
Dependencies : PSWindowsUpdate module (auto-installed)
.CHANGES
Version 1.1 (2025-10-13):
- Updated documentation structure and formatting
- Enhanced error handling and logging details
- Improved BitLocker handling with better error reporting
- Added detailed update information display (size in MB)
- Enhanced remote execution capabilities
Version 1.0 (2025-03-18):
- Initial release with core functionality
- Basic driver update functionality via Windows Update
- PSWindowsUpdate module integration
- BitLocker status checking and suspension
- Comprehensive logging implementation
.VERSION
1.1
.EXAMPLE
.\DriverUpdate.ps1
Runs the driver update process interactively, prompting user for confirmation before installing updates.
.EXAMPLE
.\DriverUpdate.ps1 -Remote
Runs the driver update process in remote/automated mode without user interaction.
.EXAMPLE
# Schedule as a task for automated driver updates
schtasks /create /tn "Driver Update" /tr "powershell.exe -ExecutionPolicy Bypass -File 'C:\Scripts\DriverUpdate.ps1' -Remote" /sc weekly /d sun /st 02:00
#>
# Add parameter block at the beginning of the script
param(
[Parameter(Mandatory=$false)]
[switch]$Remote
)
# Function to write logs
function Write-Log {
param($Message)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logMessage = "[$timestamp] $Message"
Write-Host $logMessage
Add-Content -Path "$env:ProgramData\DriverUpdate.log" -Value $logMessage
}
# Function to ensure required modules are installed
function Ensure-RequiredModules {
param (
[string[]]$ModuleNames
)
foreach ($module in $ModuleNames) {
if (-not (Get-Module -ListAvailable -Name $module)) {
Write-Log "Installing required module: $module"
try {
Install-Module -Name $module -Force -Scope CurrentUser -ErrorAction Stop
Write-Log "Successfully installed $module module"
}
catch {
Write-Log "Error installing $module module: $_"
return $false
}
}
else {
Write-Log "Required module already installed: $module"
}
Import-Module -Name $module -Force
}
return $true
}
# Check if running as administrator
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Log "Error: Script must be run as Administrator"
exit 1
}
# Check and handle BitLocker status
function Handle-BitLocker {
$bitlockerVolumes = Get-BitLockerVolume | Where-Object { $_.ProtectionStatus -eq "On" }
if ($bitlockerVolumes) {
Write-Log "BitLocker is enabled. Suspending protection..."
foreach ($volume in $bitlockerVolumes) {
try {
Suspend-BitLocker -MountPoint $volume.MountPoint -RebootCount 1
Write-Log "Suspended BitLocker for volume $($volume.MountPoint)"
}
catch {
Write-Log "Error suspending BitLocker: $_"
return $false
}
}
}
return $true
}
# Function to scan and install Windows updates
function Update-Drivers {
param(
[bool]$IsRemote = $false
)
try {
Write-Log "Checking required modules..."
if (-not (Ensure-RequiredModules -ModuleNames @('PSWindowsUpdate'))) {
Write-Log "Failed to ensure required modules are installed"
return 1
}
Write-Log "Scanning for driver updates..."
$updates = Get-WindowsUpdate -Category "Drivers" -AcceptAll
if ($updates.Count -eq 0) {
Write-Log "No driver updates found."
return $true
}
Write-Log "Found $($updates.Count) driver updates:"
Write-Log "----------------------------------------"
foreach ($update in $updates) {
Write-Log "Title: $($update.Title)"
Write-Log "KB Article: $($update.KB)"
Write-Log "Size: $([math]::Round($update.Size / 1MB, 2)) MB"
Write-Log "Description: $($update.Description)"
Write-Log "----------------------------------------"
}
if (-not $IsRemote) {
$proceed = Read-Host "Do you want to proceed with installation? (Y/N)"
if ($proceed -ne "Y") {
Write-Log "Update installation cancelled by user."
return 3
}
}
Write-Log "Starting installation of driver updates..."
if (Handle-BitLocker) {
$result = Install-WindowsUpdate -Category "Drivers" -AcceptAll -AutoReboot:$false
foreach ($update in $result) {
Write-Log "Update: $($update.Title)"
Write-Log "Status: $($update.Status)"
Write-Log "Result Code: $($update.ResultCode)"
Write-Log "----------------------------------------"
}
if ($result.RebootRequired) {
Write-Log "Updates installed. Reboot required to complete installation."
return 2
}
Write-Log "All updates installed successfully."
return 0
}
else {
Write-Log "Failed to handle BitLocker. Aborting update process."
return 1
}
}
catch {
Write-Log "Error during update process: $_"
return 1
}
}
# Main execution
Write-Log "Starting driver update process..."
$updateResult = Update-Drivers
switch ($updateResult) {
0 { Write-Log "Driver update process completed successfully" }
1 { Write-Log "Error occurred during driver update process" }
2 {
Write-Log "Updates installed successfully. System requires reboot to complete installation."
Write-Log "Please restart your computer at a convenient time."
}
3 { Write-Log "Update process cancelled by user" }
}
exit $updateResult