mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-11-29 03:13:55 +01:00
96 lines
3.2 KiB
Markdown
96 lines
3.2 KiB
Markdown
## The *save-screenshot.ps1* Script
|
||
|
||
This PowerShell script takes a single screenshot and saves it into a target folder (default is the user's screenshots folder).
|
||
|
||
## Parameters
|
||
```powershell
|
||
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
|
||
|
||
## Source Code
|
||
```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*
|