Skip to content

Instantly share code, notes, and snippets.

@AWtnb
Last active February 10, 2025 04:15
Show Gist options
  • Select an option

  • Save AWtnb/01cb32cc52f87a0890105d9c900388e5 to your computer and use it in GitHub Desktop.

Select an option

Save AWtnb/01cb32cc52f87a0890105d9c900388e5 to your computer and use it in GitHub Desktop.
PowerShellでUSBデバイスの安全な取り外し with fzf
function Get-EjectableDrive {
[OutputType([System.IO.DriveInfo])]
param()
return [System.IO.DriveInfo]::GetDrives() | Where-Object {$_.DriveType -eq [System.IO.DriveType]::Removable}
}
function Invoke-DriveEject {
try {
fzf.exe --version > $null
}
catch {
Write-Host "fzf.exe not found!"
return
}
$drives = Get-EjectableDrive | ForEach-Object {
if ($_.VolumeLabel) {
return "{0} ({1})" -f $_.Name, $_.VolumeLabel
}
return $_.Name
}
if ($drives.Length -lt 1) {
"No ejectable drive found" | Write-Host
return
}
$d = $drives | fzf.exe
if (-not $d) {
return
}
$sh = New-Object -comObject Shell.Application
$name = $d.Substring(0, $d.IndexOf("\"))
$sh.NameSpace(17).ParseName($name).InvokeVerb("Eject")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment