0

I've found an one-liner that should eject my USB stick when used in cmd window:

powershell -command "(New-Object -comObject Shell.Application).NameSpace(17).ParseName('F:').InvokeVerb('Eject')"

This unfortunately doesn't work on my machine. It just outputs an empty line. I've somehow found out that if I add the "-noexit" switch to the code, it works perfectly.

powershell -noexit -command "(New-Object -comObject Shell.Application).NameSpace(17).ParseName('F:').InvokeVerb('Eject')"

However, I want it to execute seamlessly. Is there any way to fix it?

4
  • What exactly do you mean by seamlessly? Nov 11, 2022 at 16:30
  • If I add -noexit, the code works, but it also goes into the Powershell prompt inside CMD. If I use this code in a batch file, it wont continue until I manually exit the Powershell prompt.
    – Adrian
    Nov 11, 2022 at 17:48
  • 1
    I assume you mean without leaving a PowerShell window onscreen. Workaround: There are other ways to do this, e.g., with CMD, calling DiskPart or a third-party utility, such as RemoveDrive. See superuser.com/questions/443162/… . Nov 11, 2022 at 17:53
  • @DrMoishePippik Yeah I saw that, I should've clarified that I need this to work without using any 3rd-party tools. Also, I think diskpart doesn't "eject" the way that Windows does it using the context menu.
    – Adrian
    Nov 11, 2022 at 18:49

1 Answer 1

1

Based on this answer, the following formulation might work better:

powershell.exe -Command $obj = (New-Object -comObject Shell.Application).namespace(17).ParseName('F:\');$Type = $obj.Type;while ($Type-eq 'USB Drive'){Write-Host 'Removing drive';$obj.InvokeVerb('Eject');$Type= $obj.Type}
5
  • This one doesn't work for me, even after adding -noexit.
    – Adrian
    Nov 11, 2022 at 19:48
  • The linked answer might be deprecated. I don't get why -noexit is helpful, and wonder if it's just a matter of timing. Try to chain a wait after the command, using Start-Sleep.
    – harrymc
    Nov 11, 2022 at 19:54
  • That actually works, I've chained a Start-Sleep for 2 seconds, but sometimes Windows displays a warning that the drive is currently in use and asks me to choose between Cancel, Try again and Continue. When the wait finishes, the dialog also closes and nothing happens. Is there any way to make Powershell wait for my input on the dialog?
    – Adrian
    Nov 11, 2022 at 19:58
  • The dialog might be the only thing keeping powershell alive, so it closes the moment you click. The "currently in use" problem happens frequently when some part of Windows is accessing the file, so you need to repeat. You might modify the script to eject more than once with a sleep in-between.
    – harrymc
    Nov 11, 2022 at 20:09
  • Okay, I think that's about as good as I can get it. Thank you.
    – Adrian
    Nov 11, 2022 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.