mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-12-28 01:28:50 +01:00
102 lines
3.3 KiB
Markdown
102 lines
3.3 KiB
Markdown
*save-screenshot.ps1*
|
||
================
|
||
|
||
This PowerShell script takes a single screenshot and saves it into a target folder (default is the user's screenshots folder).
|
||
|
||
Parameters
|
||
----------
|
||
```powershell
|
||
PS> ./save-screenshot.ps1 [[-TargetFolder] <String>] [<CommonParameters>]
|
||
|
||
-TargetFolder <String>
|
||
Specifies the target folder (the user's screenshots folder by default)
|
||
|
||
Required? false
|
||
Position? 1
|
||
Default value
|
||
Accept pipeline input? false
|
||
Accept wildcard characters? false
|
||
|
||
[<CommonParameters>]
|
||
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
|
||
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
|
||
```
|
||
|
||
Example
|
||
-------
|
||
```powershell
|
||
PS> ./save-screenshot
|
||
✔️ screenshot saved to C:\Users\Markus\Pictures\Screenshots\2021-10-10T14-33-22.png
|
||
|
||
```
|
||
|
||
Notes
|
||
-----
|
||
Author: Markus Fleschutz | License: CC0
|
||
|
||
Related Links
|
||
-------------
|
||
https://github.com/fleschutz/PowerShell
|
||
|
||
Script Content
|
||
--------------
|
||
```powershell
|
||
<#
|
||
.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
|
||
}
|
||
```
|
||
|
||
*(generated by convert-ps2md.ps1 using the comment-based help of save-screenshot.ps1 as of 09/20/2023 17:04:43)*
|