Tag Archive Utility

Byphunsanit

backup drivers โดย PowerShell

เหตุเกินจากที่ลง windows 11 ใหม่แบบ clean ๆ กับ USB แล้วดูเหมือนจะเรียบร้อยดีแต่ว่า มันกลับหา wifi ไม่เจอ เครื่องมันก็ไม่มีสาย lan (ตามแบบเครื่องใหม่ ๆ ที่เน้นบาง ยกง่าย) แล้วหยุดไป ต้องหาวิธีแก้ปัญหา เลยเขียน PowerShell ออกมาเพื่อ backup drivers ออกมาให้ใช้ backup ใส่ใน USB ไปเลย ก่อนที่จะล้างเครื่องใหม่อีกครั้ง
drivers_backup_utility.ps1

<#
.SYNOPSIS
    Smart Windows Drivers Backup Utility (USB Ready)
    Author: pitt.plusmagi.com pitt.plusmagi.com
#>

# 1. Check for Administrator Rights
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
    exit
}

# 2. Detect Script Location (USB Path)
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
$usbBackupPath = Join-Path -Path $scriptPath -ChildPath "Drivers_Backup"
$cBackupPath = "C:\portables\Drivers_Backup"

Clear-Host
$title = @"
====================================================
       POWERSHELL DRIVERS BACKUP UTILITY
       Author: pitt.plusmagi.com (pitt.plusmagi.com)
====================================================
"@
Write-Host $title -ForegroundColor Cyan

# 3. Choose Destination
Write-Host "Select Destination Path:" -ForegroundColor White
Write-Host "[1] USB Drive  : $usbBackupPath"
Write-Host "[2] System C:  : $cBackupPath"
$choice = Read-Host "`nSelect [1] or [2] (Default is 1)"

$targetPath = if ($choice -eq "2") { $cBackupPath } else { $usbBackupPath }

# 4. Initialize Note File
if (-not (Test-Path $targetPath)) { New-Item -ItemType Directory -Path $targetPath -Force | Out-Null }
$timestamp = Get-Date -Format "yyyy-MM-ddTHHmm"
$noteFile = Join-Path -Path $targetPath -ChildPath "note_$($timestamp).txt"
"Driver Backup Log - $(Get-Date)`r`nAuthor: pitt.plusmagi.com (pitt.plusmagi.com)`r`n" + ("="*60) | Out-File $noteFile -Encoding UTF8

# 5. Backup Process
Write-Host "`nTarget: $targetPath" -ForegroundColor Yellow
$allDrivers = Get-WindowsDriver -Online | Where-Object { $_.Inbox -eq $false }

foreach ($d in $allDrivers) {
    $categoryPath = Join-Path -Path $targetPath -ChildPath $d.ClassName
    $infFolder = Split-Path (Split-Path $d.OriginalFileName -Parent) -Leaf
    $specificBackupPath = Join-Path -Path $categoryPath -ChildPath $infFolder

    if (Test-Path $specificBackupPath) {
        Write-Host "Skipping: [$($d.ClassName)] $($d.Driver) (Already exists)" -ForegroundColor Gray
    } else {
        Write-Host "Backing up: [$($d.ClassName)] $($d.Driver) ..." -ForegroundColor Green
        if (-not (Test-Path $specificBackupPath)) { New-Item -ItemType Directory -Path $specificBackupPath -Force | Out-Null }
        pnputil.exe /export-driver $($d.Driver) "$specificBackupPath" | Out-Null
    }

    $details = "`r`nDriver: $($d.Driver)`r`nClassName: $($d.ClassName)`r`nVersion: $($d.Version)`r`nPath: $specificBackupPath`r`n" + ("-"*46)
    $details | Out-File $noteFile -Append -Encoding UTF8
}

Write-Host "`nBackup Completed!" -ForegroundColor Cyan
explorer.exe $targetPath
Pause

วิธีใช้

  1. สร้างไฟล์ drivers_backup_utility.ps1 โดยใช้ notepad แล้ว save as เป็น drivers_backup_utility.ps1 บน Desktop หรือจะใส่ใน USB เลยก็ได้
  2. ใส่เนื้อหาตามข้างบน
  3. ไปที่เมนูของ windows
  4. ช่องค้นหา พิมพ์ powershell
  5. พิมพ์ cd ตามด้วย folder ที่ save drivers_backup_utility.ps1 ไว้อย่าง
    cd /d %USERPROFILE%/Desktop
  6. ใช้โดยพิมพ์ ./drivers_backup_utility.ps1
  7. ทำตามขั้นตอนไปเรื่อย ๆ

อ่านเพิ่มเติม