The Deployment Bunny

OS Deployment, Virtualization, Microsoft based Infrastructure…

  • Archives

  • Meta

Adding drivers using PNPutil and Forfiles

Posted by Mikael Nystrom on May 7, 2011

Hey, don’t believe that this is a correct way of adding driver, but sometime you just need to be a bit a of “MacGyver” (If anyone can remember that TV Series, I’m Old so, hey I remember that anyway. For those that does not remember that it is all about being in trouble and fixing something extremely complicated to get out of trouble using simple things that you have around you and that is exactly what this is)

So, one more I’m working with a customer (still love that) and we are fooling around with drivers for different reasons, anyhow we need to load a bunch of drivers in the running OS and unfortunately you cannot use DISM.exe when the system is online for the purpose of loading drivers or remove them either for that matter, but you can use PNPUtil and that has one easy syntax, now, PNPUtil has one “small” issue, it cannot traverse folder. (Why is it never simple?)

Remembering “The good old days”

Luckily for me I’m a grove up using 8” floppy’s, so I know a bit DOS so I have done the “FOR %variable IN (set) DO command [command-parameters]” stuff a bunch of times (Credit to Björn Österman that explained this stuff many years ago). But then Microsoft started creating all the Resource kits for NT and one of the utilities in there was one of my loved ones, let me introduce Mr. ForeFiles.exe, one of of great hero’s back in the days.

Say Hi to ForFiles.exe

Forefiles.exe can basically do this “For every file that matches this criteria do the following” this is a perfect tool for automation when you need to remove, archive, delete files, like this:

forfiles.exe /P D:\ /M *.log /C “CMD /c del @Path”

That command will delete all the *.log files from the entire D:\ and all subfolders and by adding /D you can use date as a variable in many different ways, but this time we are not going to delete a bunch of files, instead we are going to add a bunch of drivers using pnputil.exe and forefiles.exe

Meet PNPUtil.exe

PNPUtil has a simple syntax and it is used to add drivers when then OS is running and here is the syntax:

Microsoft PnP Utility
Usage:
——
pnputil.exe [-f | -i] [ -? | -a | -d | -e ] <INF name>
Examples:
pnputil.exe -a a:\usbcam\USBCAM.INF      -> Add package specified by USBCAM.INF
pnputil.exe -a c:\drivers\*.inf          -> Add all packages in c:\drivers\
pnputil.exe -i -a a:\usbcam\USBCAM.INF   -> Add and install driver package
pnputil.exe -e                           -> Enumerate all 3rd party packages
pnputil.exe -d oem0.inf                  -> Delete package oem0.inf
pnputil.exe -f -d oem0.inf               -> Force delete package oem0.inf
pnputil.exe -?                           -> This usage screen

There are two things to note here

  1. The dev guy did believe that there are still floppy drives around (I’ll guess hi is as old as I am…)
  2. It does not have the traverse folder function that is needed since I don’t ever have all drivers in ONE folder

Time to for some action

By combining forfiles and pnputil we can create this neat little batch file

forfiles /p %1 /s /m *.inf /c “cmd /c pnputil -a @Path”

Save it as impdrv.cmd and run it using the following command

impdrv.cmd C:\Drivers

And it will search through the entire folder structure from C:\Drivers and add all drivers it can find on a running OS. And yes, you could use this in a task sequence to add drivers after the OS has been loaded if you need this for any reason…

/mike

19 Responses to “Adding drivers using PNPutil and Forfiles”

  1. Joel said

    This one I like! Tack så jättemycket!

  2. Alan Dooley said

    Thanks for this, useful when you notice people have built machines using SCCM but not actually checked if the drivers were in the DB…

  3. Tempusus said

    Hello,

    Have you any solution about unsigned drivers with pnputil?

  4. tzvika said

    C:\Drivers>forfiles /p C:\Drivers\drivers /s /m *.inf /c ôcmd /c pnputil -a @Pathö
    ERROR: Invalid syntax. ‘/c’ option is not allowed more than ‘1’ time(s).
    Type “FORFILES /?” for usage.

    Any suggestions?

    • justin said

      If your importing from a folder that is on the root directory, you’ll most likely receive that error message. Had the same issue, I was successful when had imported from the D: drive.

      • Mike said

        I figured it out. If you copy/paste the command from above, then it gives you the weird characters and won’t work. Have to type the command into notepad.

    • keithga said

      Mike uses smart quotes in the sample above, convert to regular double quotes around the cmd.exe parameter.

  5. briangun said

    Thanks for the write up, worked like a charm.

  6. For anyone trying to call this through another application such as SCCM on an x64 machine, You need to change the path such as:

    forfiles /p C:\Folder /s /m *.inf /c “cmd /c C:\Windows\Sysnative\pnputil.exe -a @path”

  7. Ben said

    Mike, you saved my life. thank you so much!

  8. Ben said

    For anyone who receives an error about /c, this is caused by copying and pasting the quotations from above. If you replace the quotes with new ones, it’ll work.

  9. blharding said

    This _works_! I spent hours fighting with pkgmgr to rebuild a failed disk on a Dell Latitude, and many hours searching for fixes/alternatives before I stumbled across pnputil and then your nifty method. I was just about to throw in the towel, but this just cranked through the drivers. All seems to be working except the USB is slowwwww. But, that I should be able to fix manually. Many thanks.

  10. Steve said

    Not sure when it happened but ForeFiles is included in 8.1/2012 R2 and Windows 10

  11. […] https://deploymentbunny.com/2011/05/07/adding-drivers-using-pnputil-and-forfiles/ […]

  12. jf4x said

    With PowerShell

    Get-ChildItem C:\Drivers -Recurse -Include *.inf | ForEach-Object {pnputil -i -a $_}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: