Fun PowerShell Command: Find all folders containing a file type

Me, write a short blog?  Never! I wanted to mass import all the drivers from the UBCD4win software to a custom WIM file, but the network drivers are scattered everywhere in different subfolders.  The solution?

$drv = get-childitem D:\UBCD4Win\drivers -recurse -filter "*.inf" | %{$_.DirectoryName} | Get-Unique

The first part of the command gets all the locations where an INF file lives.  Then you use a for-eachobject (%) to select the directory name of where those files are.  Get-unique automatically pulls our duplicate directories in case a directory has more than one INF file.

In the end I do the entire import in two lines of blissfully easy PowerShell code.

  1. $drv = get-childitem D:\UBCD4Win\drivers -recurse -filter "*.inf" | %{$_.DirectoryName} | Get-Unique
  2. foreach ($d in $drv){invoke-expression "D:`\WAIK`\Tools`\PETools`\peimg`.exe `/inf=$d\*`.inf D:`\PE`\winpe_x86`\mount`\Windows"}

Please note that I have an uncommon Windows Automated Installation Kit (WAIK) location, so please adjust accordingly and add quotes (`" inside inside the invoke-express) if there is a space in the path.

blog comments powered by Disqus
Tagged as: