Last active
February 10, 2025 04:15
-
-
Save AWtnb/01cb32cc52f87a0890105d9c900388e5 to your computer and use it in GitHub Desktop.
PowerShellでUSBデバイスの安全な取り外し with fzf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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