PowerShell/Scripts/save-screenshot.ps1

56 lines
2.1 KiB
PowerShell
Raw Normal View History

2021-09-27 10:38:12 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2021-10-20 13:48:38 +02:00
Saves a single screenshot
2021-07-13 21:10:02 +02:00
.DESCRIPTION
2022-12-19 07:57:35 +01:00
This PowerShell script takes a single screenshot and saves it into a target folder (default is the user's screenshots folder).
2021-10-20 13:52:08 +02:00
.PARAMETER TargetFolder
2022-12-19 07:57:35 +01:00
Specifies the target folder (the user's screenshots folder by default)
2021-07-13 21:10:02 +02:00
.EXAMPLE
2021-10-20 13:48:38 +02:00
PS> ./save-screenshot
2022-12-19 07:57:35 +01:00
screenshot saved to C:\Users\Markus\Pictures\Screenshots\2021-10-10T14-33-22.png
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-01-30 10:49:30 +01:00
.NOTES
2022-09-06 21:42:04 +02:00
Author: Markus Fleschutz | License: CC0
#>
2022-12-19 07:57:35 +01:00
param([string]$TargetFolder = "")
function GetScreenshotsFolder {
if ($IsLinux) {
$Path = "$HOME/Pictures"
if (-not(Test-Path "$Path" -pathType container)) { throw "Pictures folder at $Path doesn't exist (yet)"}
if (Test-Path "$Path/Screenshots" -pathType container) { $Path = "$Path/Screenshots" }
} else {
$Path = [Environment]::GetFolderPath('MyPictures')
if (-not(Test-Path "$Path" -pathType container)) { throw "Pictures folder at $Path doesn't exist (yet)" }
if (Test-Path "$Path\Screenshots" -pathType container) { $Path = "$Path\Screenshots" }
}
return $Path
}
2021-01-27 10:29:01 +01:00
2021-01-27 15:12:17 +01:00
function TakeScreenshot { param([string]$FilePath)
Add-Type -Assembly System.Windows.Forms
$ScreenBounds = [Windows.Forms.SystemInformation]::VirtualScreen
$ScreenshotObject = New-Object Drawing.Bitmap $ScreenBounds.Width, $ScreenBounds.Height
$DrawingGraphics = [Drawing.Graphics]::FromImage($ScreenshotObject)
$DrawingGraphics.CopyFromScreen( $ScreenBounds.Location, [Drawing.Point]::Empty, $ScreenBounds.Size)
$DrawingGraphics.Dispose()
$ScreenshotObject.Save($FilePath)
$ScreenshotObject.Dispose()
2021-01-27 10:29:01 +01:00
}
2021-01-27 10:29:01 +01:00
try {
2022-12-19 07:57:35 +01:00
if ("$TargetFolder" -eq "") { $TargetFolder = GetScreenshotsFolder }
$Time = (Get-Date)
2021-10-19 17:23:29 +02:00
$Filename = "$($Time.Year)-$($Time.Month)-$($Time.Day)T$($Time.Hour)-$($Time.Minute)-$($Time.Second).png"
2021-10-20 13:52:08 +02:00
$FilePath = (Join-Path $TargetFolder $Filename)
TakeScreenshot $FilePath
2021-10-19 17:23:29 +02:00
"✔️ screenshot saved to $FilePath"
2021-09-27 10:09:45 +02:00
exit 0 # success
2021-01-27 10:29:01 +01:00
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-01-27 10:29:01 +01:00
exit 1
}