1

I want to execute a powershell code with a batch file to take a screenshot from my desktop that i found it here Take a Screenshot of a User’s Desktop with PowerShell.

So,When i execute it as powershell script it works without any error and works for me 5/5 but when i take it as batch file like this i got this type of error that i don't know how to debug it correctly ?

Error "TerminatorExpectedAtEndOfString"

So if someone can explain me why and where this error comes from ?

Here is my batch code :

@echo off
Title Get a ScreenShot with Batch and Powershell
Call :ScreenShot
pause
::----------------------------------------------------------------------------------------------------------------------------
:ScreenShot
Powershell ^
$Path = "E:\ScreenCapture\";^
Add-Type -AssemblyName System.Windows.Forms;^
$screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds;^
$image = New-Object System.Drawing.Bitmap($screen.Width, $screen.Height);^
$graphic = [System.Drawing.Graphics]::FromImage($image);^
$point = New-Object System.Drawing.Point(0,0);^
$graphic.CopyFromScreen($point, $point, $image.Size);^
$cursorBounds = New-Object System.Drawing.Rectangle([System.Windows.Forms.Cursor]::Position,[System.Windows.Forms.Cursor]::Current.Size);^
[System.Windows.Forms.Cursors]::Default.Draw($graphic, $cursorBounds);^
$FileName = (Get-Date -F dd-MM-yyyy_HH_mm_ss)+".jpg";^
$FilePath = $Path$FileName;^
$FormatJPEG = [System.Drawing.Imaging.ImageFormat]::jpeg;^
$image.Save($FilePath,$FormatJPEG)
Exit /B
::------------------------------------------------------------------------------------------------------------------------
3
  • save the PoSh code as a file & then use powershell.exe in the BAT file to load that file. take a look at powershell.exe /? - especially the -File parameter.
    – Lee_Dailey
    Oct 29, 2021 at 9:26
  • Thank you for this suggestion, but i want to learn where this error comes from exactly?
    – Hackoo
    Oct 29, 2021 at 10:12
  • the easiest way is to test each line until you find the source. i see that Theo has done that work for you, tho. [grin]
    – Lee_Dailey
    Oct 29, 2021 at 13:02

2 Answers 2

2

May be you need:

  1. Use ^ in )

  2. Replace " to ' in $var='value'

  3. Use To.String in $((Get-Date).ToString('dd-MM-yyyy_HH_mm_ss')+'.jpg')


Test it in your command line:

Powershell $Path='E:\ScreenCapture\'; Add-Type -AssemblyName System.Windows.Forms;$screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds;$image = New-Object System.Drawing.Bitmap($screen.Width, $screen.Height^);$graphic = [System.Drawing.Graphics]::FromImage($image^);$point = New-Object System.Drawing.Point(0,0^);$graphic.CopyFromScreen($point, $point, $image.Size^);$cursorBounds = New-Object System.Drawing.Rectangle([System.Windows.Forms.Cursor]::Position,[System.Windows.Forms.Cursor]::Current.Size^);[System.Windows.Forms.Cursors]::Default.Draw($graphic, $cursorBounds^);$FileName = $((Get-Date^).ToString('dd-MM-yyyy_HH_mm_ss'^)+'.jpg'^);$FilePath = $Path+$FileName;$FormatJPEG = [System.Drawing.Imaging.ImageFormat]::jpeg;$image.Save($FilePath,$FormatJPEG^)

Test it in your bat file:

@echo off
Title Get a ScreenShot with Batch and Powershell
Call :ScreenShot
pause 
goto :eof
::----------------------------------------------------------------------------------------------------------------------------
:ScreenShot
Powershell $Path='E:\ScreenCapture\';^
Add-Type -AssemblyName System.Windows.Forms;^
$screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds;^
$image = New-Object System.Drawing.Bitmap($screen.Width, $screen.Height^);^
$graphic = [System.Drawing.Graphics]::FromImage($image^);^
$point = New-Object System.Drawing.Point(0,0^);^
$graphic.CopyFromScreen($point, $point, $image.Size^);^
$cursorBounds = New-Object System.Drawing.Rectangle([System.Windows.Forms.Cursor]::Position,[System.Windows.Forms.Cursor]::Current.Size^);^
[System.Windows.Forms.Cursors]::Default.Draw($graphic, $cursorBounds^);^
$FileName = $((Get-Date^).ToString('dd-MM-yyyy_HH_mm_ss'^)+'.jpg'^);^
$FilePath = $Path+$FileName;$FormatJPEG = [System.Drawing.Imaging.ImageFormat]::jpeg;^
$image.Save($FilePath,$FormatJPEG^)

0
2

The error comes from this line:

$FilePath = $Path$FileName;^

You should not concatenate variables to form a file path like that.
It would work if you surround it with double-quotes like this: $FilePath = "$Path$FileName";^, but far, FAR better is to use the Join-Path cmdlet:

$FilePath = Join-Path -Path $Path -ChildPath $FileName;^

or if you rather use .Net directly:

$FilePath = [System.IO.Path]::Combine($Path, $FileName);^
3
  • Thank you for your reply, but i don't know why none of those does not worked for me ? but this work 5/5 Powershell -ExecutionPolicy Bypass -C ".\ScreenShot-PS.ps1" So my problem is not solved yet !
    – Hackoo
    Oct 29, 2021 at 14:53
  • @Hackoo use -File instead of -Command
    – Theo
    Oct 29, 2021 at 15:00
  • No problem for those two commands they worked Powershell -ExecutionPolicy Bypass -File ".\ScreenShot-PS.ps1" or Powershell -ExecutionPolicy Bypass -Command ".\ScreenShot-PS.ps1" works 5/5, but i have a feeling this comes from Enconding or something from the parser of my console command line ?? still searching for it ! Can you please edit your post and send me the whole code that worked for you! thank you !
    – Hackoo
    Oct 29, 2021 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.