Author Posts

January 1, 2012 at 12:00 am

by cswan at 2013-03-02 22:14:21

I am copying files from multiple USB memory cards to the hard drive. With the USB memory card installed, Drive G: shows in the list generated by Get-PSDrive and is found by Test-Path. Once the USB drive has been ejected and/or physically removed, Test-Path no longer finds the drive as expected. When the memory card is reinstalled, the drive again shows up in the list returned by Get-PSDrive, but Test-Path does not find the drive and I am unable to access files on the drive. Stopping and restarting the program fixes everything until the card is removed again. Suggestions on how to fix the problem? Thanks for your help!

While ($true){ #loop forever
$DriveList = @(Get-PSDrive -PSProvider 'FileSystem')
Write-Output "$driveList"
IF (!(Test-Path G:)){Write-Output "drive not found"}
sleep 2
}

Code Output:

C D E F G #——–drive present
C D E F #——–drive ejected
drive not found
C D E F
drive not found
C D E F
drive not found
C D E F G #——–drive reinstalled, but not found by Test-Path
drive not found

by DonJ at 2013-03-02 22:35:17

I think you're running into limitation on how PowerShell enumerates drives. Try mapping a PSDrive to the physical drive letter?