FileRepository data store

Removing Old and Unused Drivers from Driver Store using Powershell


Each time you install or update your device driver, Windows OS (since Vista) continues to store the old version of the driver in the system Driver Store. Thereby, if the system doesn’t work correctly with the new driver, user can roll back to an older version of the driver at any moment.

Windows Driver Store is located in the folder C:\windows\system32\DriverStore\FileRepository. However, some Windows users began to notice that over time, the folder %windir%\system32\DriverStore\FileRepository begins to occupy more and more disk space. In our example, the size of this folder is more than 11 GB. Quite a lot, especially for the SSD drive!

FileRepository

How to Remove Old and Unused Drivers from Driver Store?

To clear the contents of folder FileRepository from the outdated drivers, we prepared a small PowerShell script, that removes all duplicates drivers except the drivers with the latest date.

Warning! In any case don’t delete any files or folders manually from the directory FileRepository!

Important Tips:

  • To run this script you need to update your PowerShell version to Windows Management Framework 1 (or at least WMF4).
  • Before running the script, make sure you have the current drivers for the video card, printer and other devices, that are constantly in use. In case of problems, you can always re-install properly driver.
  • Also it is recommended to create a restore point. To do this, run the command:
    Checkpoint-Computer -Description “PointBeforeDeleteUnusedDrivers”

    Verify that the restore point was created successfully using the command:

    Get-ComputerRestorePoint

We will get the list of third-party drivers installed in the system by using the DISM command:

dism /online /get-drivers

Then parse the output using PowerShell, select the driver duplicates and sort them by date. From the resulting list we will exclude the most recent version for each driver.

READ ALSO  Add Calendar Permissions in Office 365 via Powershell

After that, remove remaining drivers using pnputil utility. In some cases, if some driver is not removed, add the -f switch.

The script is as follows:

$dismOut = dism /online /get-drivers
$Lines = $dismOut | select -Skip 10
$Operation = "theName"
$Drivers = @()
foreach ( $Line in $Lines ) {
    $tmp = $Line
    $txt = $($tmp.Split( ':' ))[1]
    switch ($Operation) {
        'theName' { $Name = $txt
                     $Operation = 'theFileName'
                     break
                   }
        'theFileName' { $FileName = $txt.Trim()
                         $Operation = 'theEntr'
                         break
                       }
        'theEntr' { $Entr = $txt.Trim()
                     $Operation = 'theClassName'
                     break
                   }
        'theClassName' { $ClassName = $txt.Trim()
                          $Operation = 'theVendor'
                          break
                        }
        'theVendor' { $Vendor = $txt.Trim()
                       $Operation = 'theDate'
                       break
                     }
        'theDate' { # change the date format for easy sorting
                     $tmp = $txt.split( '.' )
                     $txt = "$($tmp[2]).$($tmp[1]).$($tmp[0].Trim())"
                     $Date = $txt
                     $Operation = 'theVersion'
                     break
                   }
        'theVersion' { $Version = $txt.Trim()
                        $Operation = 'theNull'
                        $params = [ordered]@{ 'FileName' = $FileName
                                              'Vendor' = $Vendor
                                              'Date' = $Date
                                              'Name' = $Name
                                              'ClassName' = $ClassName
                                              'Version' = $Version
                                              'Entr' = $Entr
                                            }
   
                        $obj = New-Object -TypeName PSObject -Property $params
                        $Drivers += $obj
                        break
                      }
         'theNull' { $Operation = 'theName'
                      break
                     }
    }
}
Write-Host "All installed third-party  drivers"
$Drivers | sort Filename | ft
Write-Host "Different versions"
$last = ''
$NotUnique = @()
foreach ( $Dr in $($Drivers | sort Filename) ) {
   
    if ($Dr.FileName -eq $last  ) {  $NotUnique += $Dr  }
    $last = $Dr.FileName
}
$NotUnique | sort FileName | ft
Write-Host "Outdated drivers"
$list = $NotUnique | select -ExpandProperty FileName -Unique
$ToDel = @()
foreach ( $Dr in $list ) {
    Write-Host "duplicate found" -ForegroundColor Yellow
    $sel = $Drivers | where { $_.FileName -eq $Dr } | sort date -Descending | select -Skip 1
    $sel | ft
    $ToDel += $sel
}
Write-Host "Drivers to remove" -ForegroundColor Red
$ToDel | ft
# removing old drivers
foreach ( $item in $ToDel ) {
    $Name = $($item.Name).Trim()
    Write-Host "deleting $Name" -ForegroundColor Yellow
    Write-Host "pnputil.exe -d $Name" -ForegroundColor Yellow
    Invoke-Expression -Command "pnputil.exe -d $Name"
}

Tip. In case you need to show the list of unused drivers only, comment Invoke-Expression (add a # character before the command Invoke-Expression -Command “pnputil.exe -d $Name)

Copy the code of the script and save it to file drv_cleanup.ps1 (to the folder c:\ps). Open the elevated PowerShell console and allow to execute the unsigned scripts:

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

Next run the script: C:\PS\drv_cleanup.ps1

READ ALSO  How to Install Device Drivers During Windows 8 Install Unattended

execution policy powershell

The script will remove unused drivers. After its completion, restart your computer and check if everything works properly and, if necessary, reinstall the appropriate driver.

In our case, after the execution of the script the size of a folder FileRepository reduced from 11 to 1.5 GB. Not bad!


You may also like:

Add Calendar Permissions in Office 365 via Powersh... This is a tutorial on how to add calendar permissions in Office 365 for your users via Powershell. You can add permissions onto a specific mailbox, or...
Download Dell Drivers the Easy Way via FTP Site Whether you work at a Helpdesk or are an IT Administrator, a lot of our readers download drivers from the Dell website a lot. I know I am personally d...
Lenovo Yoga 13 Wifi Keeps Disconnecting Lenovo Yoga We have been dealing with some Lenovo Yoga 13 ultrabooks recently and the wireless cards keep disconnecting and then refuse to reconnect....
Grant Full Access to All Mailboxes in Exchange 200... This is a short tutorial on how to add full access to all mailboxes in Exchange 2007 and 2010 for a an additional user. This can come in handy when sa...
How to copy files with BITS using PowerShell If you use local (and global) networks, you might know that files between systems are transferring by using SMB, FTP or HTTP protocols. The problem wi...