PowerShell: determine which application prevents an USB drive from being ejected
When trying to eject an external USB drive («Safely remove hardware and eject media»), Windows might respond with the message Problem Ejecting USB Mass Storage Device (This device is currently in use. Close any programs or windows that might be using the device, and then try again):
Unfortunately, the message does not reveal which program or application uses the USB drive. With PowerShell, it is possible to determine this application with the get-eventLog cmdLet:
BTW, this information can also be determined with the Event viewer (eventvwr.exe) by going to Windows Logs -> System -> Filter Current Log and the setting the Event ID to 225:
Using System call NtQueryInformationFile() with parameter FileProcessIdsUsingFileInformation
The blocking process can also be found by calling NtQueryInformationFile() (Windows/dirs/Windows/System32/ntdll_dll) with the parameter FileProcessIdsUsingFileInformation (=47).