I wanted to share a simple PowerShell script that you can use within Pulseway Automation to remotely eject any USB storage devices connected to a system. This can be helpful for security-conscious environments or in scenarios where USB usage needs to be tightly controlled.
Works well in combination with the Disable USB Storage Access script in the built in category.
Â
The script uses Shell.Application to trigger the native "Eject" action for removable drives (DriveType = 2). Here's the version that loops through all mounted USB volumes:
✅ Works great in Pulseway automation tasks
💡 You can modify it to eject a specific drive letter if needed
powershell
# Eject all USB volumes using Shell.Application COM object
$Eject = New-Object -ComObject Shell.Application
# Get all removable (DriveType = 2) volumes with drive letters
$volumes = Get-WmiObject -Class Win32_Volume | Where-Object { $_.DriveType -eq 2 -and $_.DriveLetter }
foreach ($vol in $volumes) {
Write-Host "Ejecting drive $($vol.DriveLetter)..."
$Eject.NameSpace(17).ParseName($vol.DriveLetter).InvokeVerb("Eject")
}
Â
Notes:
During my testing, I've seen that USB devices will not be removed if a process is running from the disks in question.
In other words, if there is a write/read process running, the disk is not ejected.
Â
Let me know if you have questions or enhancements!
Hi everyone,
I wanted to share a simple PowerShell script that you can use within Pulseway Automation to remotely eject any USB storage devices connected to a system. This can be helpful for security-conscious environments or in scenarios where USB usage needs to be tightly controlled.
Works well in combination with the Disable USB Storage Access script in the built in category.
Â
The script uses
Shell.Applicationto trigger the native "Eject" action for removable drives (DriveType = 2). Here's the version that loops through all mounted USB volumes:# Eject all USB volumes using Shell.Application COM object $Eject = New-Object -ComObject Shell.Application # Get all removable (DriveType = 2) volumes with drive letters $volumes = Get-WmiObject -Class Win32_Volume | Where-Object { $_.DriveType -eq 2 -and $_.DriveLetter } foreach ($vol in $volumes) { Write-Host "Ejecting drive $($vol.DriveLetter)..." $Eject.NameSpace(17).ParseName($vol.DriveLetter).InvokeVerb("Eject") }Â
Notes:
During my testing, I've seen that USB devices will not be removed if a process is running from the disks in question.
In other words, if there is a write/read process running, the disk is not ejected.
Â
Let me know if you have questions or enhancements!