I would simplify this a few different ways and also move the definition of the backup path and image magick executable to ensure it’s defined in the scriptblock. I’ve tested this and it works perfectly.
If($FileWatcher){$FileWatcher.Dispose();$FileWatcher = $null}
$FileWatcher = New-Object System.IO.FileSystemWatcher -Property @{
NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
Path = 'C:\ImageTest\Source\'
}
Powershell is case sensitive by default so you can drop ToLower. You can also move the file during the conversion process and omit the copy portion.
Register-ObjectEvent -InputObject $FileWatcher -EventName Created -Action {
$BackupPath = 'C:\ImageTest\Backup'
$im_exe = "C:\Program Files\ImageMagick-7.0.10-Q16-HDRI\magick.exe"
#get extension type
$extn = [IO.Path]::GetExtension($event.SourceEventArgs.FullPath)
#look for jpeg and .jpg only
If ($extn -match 'jpg|jpeg'){
#get file name
Write-Host $event.SourceEventArgs.Name
#call imgagemagick pass file to process and move in same step
&$im_exe $event.SourceEventArgs.FullPath -resize 50% "$BackupPath\new_$($event.SourceEventArgs.Name)"
}
}