Are there Powershell cmdlets that install and remove Windows drivers?

前端 未结 3 632
天涯浪人
天涯浪人 2021-02-10 04:15

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 \"Inst

相关标签:
3条回答
  • 2021-02-10 04:50

    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
    
    0 讨论(0)
  • 2021-02-10 04:59

    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).

    0 讨论(0)
  • 2021-02-10 05:06

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

    pnputil /add-driver "path\to\driver\*inf" /install
    
    0 讨论(0)
提交回复
热议问题