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?

Adrian
  • 1

1 Answers1

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}
harrymc
  • 498,455