mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-12-15 19:21:04 +01:00
56 lines
2.1 KiB
PowerShell
Executable File
56 lines
2.1 KiB
PowerShell
Executable File
<#
|
|
.SYNOPSIS
|
|
Saves a single screenshot
|
|
.DESCRIPTION
|
|
This PowerShell script takes a single screenshot and saves it into a target folder (default is the user's screenshots folder).
|
|
.PARAMETER TargetFolder
|
|
Specifies the target folder (the user's screenshots folder by default)
|
|
.EXAMPLE
|
|
PS> ./save-screenshot
|
|
✔️ screenshot saved to C:\Users\Markus\Pictures\Screenshots\2021-10-10T14-33-22.png
|
|
.LINK
|
|
https://github.com/fleschutz/PowerShell
|
|
.NOTES
|
|
Author: Markus Fleschutz | License: CC0
|
|
#>
|
|
|
|
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
|
|
}
|
|
|
|
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()
|
|
}
|
|
|
|
try {
|
|
if ("$TargetFolder" -eq "") { $TargetFolder = GetScreenshotsFolder }
|
|
$Time = (Get-Date)
|
|
$Filename = "$($Time.Year)-$($Time.Month)-$($Time.Day)T$($Time.Hour)-$($Time.Minute)-$($Time.Second).png"
|
|
$FilePath = (Join-Path $TargetFolder $Filename)
|
|
TakeScreenshot $FilePath
|
|
|
|
"✔️ screenshot saved to $FilePath"
|
|
exit 0 # success
|
|
} catch {
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
exit 1
|
|
}
|