0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Win11] PowerShellでscreenshot保存(Snipping tool)

Posted at

WIn11のスクリーンショットを用いてキャプチャをするが,毎回powerpointに貼り付けて,画像として保存していた.
面倒なので,CLIで操作できたら便利(少なくとも,今までよりは良い)と考えた.
しかし,スクリーンショットやキャプチャの為に新規の外部ツールを利用する事は嫌なので,Powershellで実行できる方法を調べた.

In a nutshell

Windows標準のSnipping Toolを用いてスクリーンショットを取得し,PowerShellやタスクスケジューラを使って自動保存する方法を紹介する.
手動操作ではPowerShellスクリプトを実行し,タスクスケジューラを使うことで自動化が可能になる.

Print Screen(Snipping tool) の基本情報

厳密には,PrintScreenとSnipping tool は異なるが,今回はまとめてSnipping toolとする.
これは,Windowsに標準搭載されているスクリーンショット取得ツールである.これは,GUIを介して操作ができる.
Win + Shift + S ,または,PrtScr キーで起動できる.
キャプチャ画像はクリップボードにコピーされる.

Tool barから操作ができる.
screen_toolbar.png
画像は参考文献1より,マイナビニュース で公開されている画像を修正(切り抜き)した

手動 PowerShellを用いる

手順
Win + Shift + S を押して,範囲選択でスクリーンショットを撮る(クリップボードに保存).

PowerShell を開いて,以下のスクリプトを実行する

PrtSc 以降にコピー操作を行うと,画像データがクリップボードから削除され,スクリプトが正常に動作しない.

if ($clipboardData -ne $null -and $clipboardData.GetDataPresent([System.Windows.Forms.DataFormats]::Bitmap)) {
    $img = $clipboardData.GetData([System.Windows.Forms.DataFormats]::Bitmap)
    $savePath = "C:\Users\$env:USERNAME\Pictures\screenshot.png"
    $img.Save($savePath, [System.Drawing.Imaging.ImageFormat]::Png)
    Write-Output "Screenshot saved to $savePath"
} else {
    Write-Output "クリップボードに画像データがありません.最後にPrtScrを実行してください.PrtScr以降にコピーを行わないでください."
}

自動化 タスクスケジューラを用いる

以下のPSファイルを作成する

save_screenshot.ps1

Add-Type -AssemblyName System.Windows.Forms
Start-Sleep -Milliseconds 500 
$img = [System.Windows.Forms.Clipboard]::GetImage()

if ($img -ne $null) {
    $timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
    $path = "C:\Users\$env:USERNAME\Pictures\screenshot_$timestamp.png"
    $img.Save($path, [System.Drawing.Imaging.ImageFormat]::Png)
    Write-Output "Screenshot saved to: $path"
} else {
    Write-Output "No image found in clipboard."
}

Start-Sleep -Milliseconds 500は,スクリーンショットの処理が完了する前に PowerShell スクリプトが動作してしまうのを防ぐ為

続いて,batファイルを作成する

save_screenshot.bat

@echo off
powershell -ExecutionPolicy Bypass -File "%USERPROFILE%\save_screenshot.ps1"
exit

上記の2つのファイルのディレクトリは以下が推奨

C:\Users\username\save_screenshot.ps1
C:\Users\username\save_screenshot.bat

タスクスケジューラに登録する

Win + R -> taskschd.msc を入力 -> タスクスケジューラ を開く.
[基本タスクの作成] をクリック.
名前: PrintScreen Auto Save
トリガー を [新しいイベントで開始] に設定.
イベントを設定
ログ: Application
ソース: SnippingTool
イベントID: 1001
操作 -> プログラムの開始
プログラム/スクリプト: cmd
引数の追加: /c "%USERPROFILE%\save_screenshot.bat"

注意として,イベントID 1001 は環境によって違う事がある.

Summary

  • PowerShellを使用すると,スクリーンショットを手動でファイルに保存できる.
  • タスクスケジューラを利用すれば,Snipping Toolのイベント発生時に自動保存が可能.

References

  1. マイナビニュース編集部. (2023). マイナビニューストップ > +Digital >パソコン >Windows >PrintScreenキーの設定を「Alt」+「PrtSc」キーに戻す
    https://news.mynavi.jp/article/win11tips-201/

  2. Microsoft公式ドキュメント: Snipping Tool を使ってスクリーン ショットをキャプチャする
    Microsoft公式ドキュメント: Snipping Tool を使ってスクリーン ショットをキャプチャする

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?