-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExport Drivers.ps1
More file actions
74 lines (62 loc) · 2.49 KB
/
Export Drivers.ps1
File metadata and controls
74 lines (62 loc) · 2.49 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
<#
.SYNOPSIS
Exports all installed Windows drivers to a folder named after the device's serial number.
.DESCRIPTION
This script performs the following actions:
- Checks if it's running with Administrator privileges; if not, it restarts itself elevated.
- Determines the script's starting location.
- Retrieves the device's serial number using WMI.
- Deletes any existing driver export folder with the same serial number.
- Creates a new folder named "Drivers_<SerialNumber>" in the script's directory.
- Exports all installed drivers to that folder using Export-WindowsDriver.
.NOTES
- Requires Administrator privileges.
- Compatible with Windows 10/11 and Server editions that support Export-WindowsDriver.
- Must be run in an elevated PowerShell session or will auto-restart elevated.
.AUTHOR
Ronald van Heugten
#>
# Ensure errors are shown
$ErrorActionPreference = "Stop"
# 1. Check if running as Administrator
function Test-IsAdmin {
$currentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal($currentUser)
return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
if (-not (Test-IsAdmin)) {
Write-Host "Restarting script with administrator privileges..."
Start-Process powershell "-ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
exit
}
# 2. Get script start location
$scriptPath = $MyInvocation.MyCommand.Path
$scriptDir = Split-Path -Parent $scriptPath
# 3. Get device serial number
try {
$serialNumber = (Get-WmiObject -Class Win32_BIOS).SerialNumber.Trim()
} catch {
Write-Error "Failed to retrieve serial number: $_"
exit 1
}
# 4. Prepare driver export folder
$driverExportDir = Join-Path -Path $scriptDir -ChildPath "Drivers_$serialNumber"
# If folder exists, delete it first
if (Test-Path -Path $driverExportDir) {
try {
Remove-Item -Path $driverExportDir -Recurse -Force
Write-Host "Existing driver folder removed: $driverExportDir"
} catch {
Write-Error "Failed to remove existing folder: $_"
exit 1
}
}
# Create fresh folder
New-Item -Path $driverExportDir -ItemType Directory | Out-Null
# 5. Export installed drivers
try {
Export-WindowsDriver -Online -Destination $driverExportDir
Write-Host "Drivers successfully exported to: $driverExportDir"
} catch {
Write-Error "Driver export failed: $_"
}