Are there Powershell cmdlets that install and remove Windows drivers?

22,601

Solution 1

Not only are there not PowerShell cmdlets for this, it seems there isn't even managed code to do it within the .Net framework (what follows is basically a translation of that answer into PowerShell).

Luckily, the .Net framework can call windows APIs through platform invoke (p/invoke), and PowerShell can do so too.

The linked answer shows how to do it in C#. To do it powershell we'll use the same signature that was generated in that answer, and use it with the Add-Type cmdlet (see example 5) to make it available to your script.

$signature = @"
[DllImport("Setupapi.dll", EntryPoint="InstallHinfSection", CallingConvention=CallingConvention.StdCall)]
public static extern void InstallHinfSection(
    [In] IntPtr hwnd,
    [In] IntPtr ModuleHandle,
    [In, MarshalAs(UnmanagedType.LPWStr)] string CmdLineBuffer,
    int nCmdShow);
"@
$Win32Functions = Add-Type -MemberDefinition $signature -UsingNamespace System.Runtime.InteropServices -Name Win32SInstallHinfSection -Namespace Win32Functions -PassThru 

$Win32Functions::InstallHinfSection([IntPtr]::Zero, [IntPtr]::Zero, "<section> <mode> <path>", 0)

See the MSDN documentation for InstallHinfSection for details on the parameters (particularly the string format).

Solution 2

There is a PowerShell module named DeviceManagement, installable from the PowerShell Gallery via Install-Module, which contains the Install-DeviceDriver cmdlet. Example:

Install-Module -Name DeviceManagement -Force
Install-DeviceDriver -InfFilePath C:\Drivers\LAN\acmelan.inf
Get-Device | Where-Object Name -like 'ACME Network Adapter*' | Select-Object Name,DriverProvider,DriverVersion,DriverDescription,HasProblem

Solution 3

I have to add you can call the pnputil.exe command:

pnputil /add-driver "path\to\driver\*inf" /install
Share:
22,601
alx9r
Author by

alx9r

Updated on August 25, 2020

Comments

  • alx9r
    alx9r over 3 years

    Note: For this question, when I refer to "Windows drivers" I mean .inf and associated files which can otherwise be installed by right-clicking the .inf and clicking "Install" in Windows Explorer. I do not mean any kind of setup.exe-style executable which might install a driver.


    There exists the following:

    I have not, however, found a corresponding Powershell Cmdlet that supports installing and uninstalling drivers on the running system. I'm sure I could wrap dpinst.exe in some powershell, but I'd like to avoid mapping command line parameters and parsing output if a more Powershell-native method exists.

    Do Powershell Cmdlets exist that install and uninstall Windows drivers on the running system? Is there some other way to install and uninstall Windows drivers using Powershell that does not involve dpinst.exe?

  • alx9r
    alx9r over 8 years
    InstallHinfSection seems to have a void return value and no return parameters. Should I expect exceptions from InstallHinfSection that can be caught in powershell?
  • briantist
    briantist over 8 years
    @alx9r that's a very good question, which I don't have the answer to unfortunately. I recommend trying to force an exception to be thrown, maybe by feeding it invalid data, and see what it does.
  • Harry Johnston
    Harry Johnston over 3 years
    No Windows API calls generate C++ exceptions, because they may be called from other languages such as C. (At a guess, InstallHinfSection will present a dialog box to the user if an error occurs.)