Saturday, May 5, 2018

Eject usb drive but not all of them

Eject usb drive but not all of them

Using the windows 10 icon, "eject the media", is slow because windows start all my sleeping usb drives before i can click "eject safely"

I wanted to eject some drive without waiting for the windows to open
I did not want to eject my sleeping usb drives that i use for storage

So, I designed this little powershell script to do so

##### powershell script #####

### requirements
# because this script is not signed
# (this will allow unsigned local powershell scripts to run)
#   windows 7
#     Set-ExecutionPolicy RemoteSigned #################################
#   windows 10
#     Set-ExecutionPolicy -scope CurrentUser RemoteSigned ##############
#   run it in powershell ISE interface runned as admin


$donotejectserials = @(`
"FAFFFFF0FDF2F7F1BFC15635",`
"2GEWLRPL",`
"14510E6CDA88")

# get serial i do not want to eject - list all usb drives
#get-Disk | Where {($_.BusType -eq "USB")}

$allusbdisk = get-Disk | Where {($_.BusType -eq "USB")}

foreach ($usbdisk in $allusbdisk)
    {
    $founddonoteject = 0
    foreach ($donotejectserial in $donotejectserials)
        {
        if ($usbdisk.serialnumber -like "*$donotejectserial*")
            {
            $founddonoteject = 1
            }
        #write-host "Serials: " $usbdisk.serialnumber " ----- " $donotejectserial " ----- " $founddonoteject
        }
    if ($founddonoteject -eq 0)
        {
       
        $letter = ($usbdisk | Get-Partition ).DriveLetter
        write-host "Ejecting: " $letter " ----- " $usbdisk.friendlyname " ----- " $usbdisk.serialnumber
        (New-Object -comObject Shell.Application).NameSpace(17).ParseName("$($letter):").InvokeVerb('Eject')
        }
    else
        {
        #write-host "NOT Ejecting: " $usbdisk.friendlyname
        }
    }

14 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. I appreciate that you produced this wonderful article to help us get more knowledge about this topic. I know, it is not an easy task to write such a big article in one day, I've tried that and I've failed. But, here you are, trying the big task and finishing it off and getting good comments and ratings. That is one hell of a job done!
    java training in chennai | java training in bangalore


    java training in tambaram | java training in velachery

    ReplyDelete
  4. Really you have done great job,There are may person searching about that now they will find enough resources by your post
    python training in pune | python training institute in chennai | python training in Bangalore

    ReplyDelete
  5. Nice tutorial. Thanks for sharing the valuable information. it’s really helpful. Who want to learn this blog most helpful. Keep sharing on updated tutorials…
    Best Devops Training in pune
    Devops interview questions and answers

    ReplyDelete
  6. Great job for publishing such a beneficial web site. Your web log isn’t only useful but it is additionally really creative too. There tend to be not many people who can certainly write not so simple posts that artistically. Continue the nice writing cursos de ti online

    ReplyDelete
  7. I think other website proprietors should take this internet site as an example , very clean and fantastic user genial style . Read More Here

    ReplyDelete
  8. Bet on Blackjack, with the best rules and rules - WGRi
    In Blackjack, there are four ways to play, but the dealer who is the highest-charted Blackjack player is the best. 바카라 He is kadangpintar the best player in the game. If the 1xbet dealer who

    ReplyDelete