2020/03/13 追記
powershell 7 で [Windows.Forms.Clipboard]::GetImage()
が利用可能になったので下記内容は不要になりました。
結論:旧来の Windows PowerShell を使用します。
コード
function Convert-ClipboardImage2File {
<#
.SYNOPSIS
クリップボード内の画像をファイルに保存する
.PARAMETER basename
ファイル名(拡張子以外)
.PARAMETER extension
拡張子
#>
param (
[string]$basename,
[string]$extension = ".png"
)
if (-not $basename) {
$t = Get-Date
$basename = "{0:d4}{1:d2}{2:d2}{3:d2}{4:d2}{5:d2}" -f $t.Year, $t.Month, $t.Date, $t.Hour, $t.Minute, $t.Second
}
$fullpath = Join-Path $pwd.Path -ChildPath ($basename + $extension)
if (Test-Path $fullpath) {
Write-Host ("'{0}{1}' already exists!" -f $basename, $extension) -ForegroundColor Magenta
return
}
$execute = (powershell.exe "Get-Clipboard -Format Image|Set-Variable img; if(-not `$img){return}; `$img.save('$fullpath'); return 1")
if (-not $execute){
Write-Host "ERROR: no image in clipboard..." -ForegroundColor Magenta
return
}
Write-Host "save clipboard image as " -NoNewline
Write-Host ("'{0}{1}'" -f $basename, $extension) -ForegroundColor Cyan
}
コマンド自体を文字列として powershell の引数に渡し、その標準出力を $execute
に格納しています。
クロスプラットフォームになるなどで期待の大きい pwsh ですが、主な用途が文字列加工や検索といった事務作業だった身としてはクリップボード機能が使えなくなったのは痛いですね…。