Skip to content

Instantly share code, notes, and snippets.

@rgl
Created November 25, 2019 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgl/51de05f26254cf5f636f1c5b94e19eb8 to your computer and use it in GitHub Desktop.
Save rgl/51de05f26254cf5f636f1c5b94e19eb8 to your computer and use it in GitHub Desktop.
Install windows drivers

Example of how to install a inf file:

NB You can also use the dism module cmdlets (e.g. Add-WindowsDriver).

function Install-Driver($path) {
    # trust the driver certificate.
    $catPath = $path.Replace('.inf', '.cat')
    $cerPath = $path.Replace('.inf', '.cer')
    $certificate = (Get-AuthenticodeSignature $catPath).SignerCertificate
    [System.IO.File]::WriteAllBytes($cerPath, $certificate.Export('Cert'))
    Import-Certificate -CertStoreLocation Cert:\LocalMachine\TrustedPublisher $cerPath | Out-Null

    # install the driver.
    pnputil -i -a $path
    if ($LASTEXITCODE) {
        throw "Failed with exit code $LASTEXITCODE"
    }
}

[IO.Compression.ZipFile]::ExtractToDirectory('C:\Windows\Temp\virtio\virtio.zip', 'C:\Windows\Temp\virtio')
$virtioDestinationDirectory = "$env:ProgramFiles\virtio"
Get-ChildItem -Recurse -File C:\Windows\Temp\virtio\drivers.tmp | ForEach-Object {
    $driverName = $_.Directory.Parent.Parent.Name
    $driverSourceDirectory = $_.Directory
    $driverDestinationDirectory = "$virtioDestinationDirectory\$driverName"
    if (Test-Path $driverDestinationDirectory) {
        return
    }
    Write-Host "Installing the $driverName driver..."
    mkdir -Force $driverDestinationDirectory | Out-Null
    Copy-Item "$driverSourceDirectory\*" $driverDestinationDirectory
    Install-Driver (Resolve-Path "$driverDestinationDirectory\*.inf").Path
}

Write-Host 'Installing the Balloon service...'
&"$virtioDestinationDirectory\Balloon\blnsvr.exe" -i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment