Skip to content

Instantly share code, notes, and snippets.

@soruly
Last active May 11, 2025 04:15
Show Gist options
  • Save soruly/889d926fcd4b2b8e8dc113a100b9d988 to your computer and use it in GitHub Desktop.
Save soruly/889d926fcd4b2b8e8dc113a100b9d988 to your computer and use it in GitHub Desktop.
take screenshot of desktop every 15 seconds
Add-Type -AssemblyName System.Drawing
$timer = New-Object timers.timer
$timer.Interval = 15000
$timer.Enabled = $true
$timer.start()
Register-ObjectEvent -InputObject $timer -EventName Elapsed -SourceIdentifier PrintScreen -Action {
$folderName = Get-Date -Format "yyyy-MM-dd"
$fileName = "$(Get-Date -Format "yyyy-MM-dd HH mm ss").jpg"
$folderPath = Join-Path "C:\printscreendiary" $folderName
$filePath = Join-Path $folderPath $fileName
[void](New-Item -ItemType Directory -Force -Path $folderPath)
# https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-videocontroller
$vc = Get-WmiObject Win32_VideoController | Where-Object -Property Availability -eq 3
$width = [convert]::ToInt32($vc.CurrentHorizontalResolution)
$height = [convert]::ToInt32($vc.CurrentVerticalResolution)
$bmp = New-Object System.Drawing.Bitmap $width, $height
$p0 = New-Object System.Drawing.Point 0, 0
$size = New-Object System.Drawing.Size $width, $height
$graphics = [System.Drawing.Graphics]::FromImage($bmp)
$graphics.CopyFromScreen($p0, $p0, $size)
$graphics.Dispose()
$encoderParamSet = New-Object System.Drawing.Imaging.EncoderParameters(1)
$encoderParamSet.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter([System.Drawing.Imaging.Encoder]::Quality, 90)
$jpgCodec = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() | Where-Object { $_.MimeType -eq 'image/jpeg' }
$bmp.Save($filePath , $jpgCodec, $encoderParamSet)
$bmp.Dispose()
}
# Get-EventSubscriber
# Unregister-Event -SourceIdentifier PrintScreen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment