param([Parameter(Mandatory=$true)][string]$Path) Add-Type -AssemblyName System.Runtime.WindowsRuntime [Windows.Storage.StorageFile, Windows.Storage, ContentType=WindowsRuntime] | Out-Null [Windows.Graphics.Imaging.BitmapDecoder, Windows.Graphics.Imaging, ContentType=WindowsRuntime] | Out-Null [Windows.Media.Ocr.OcrEngine, Windows.Foundation, ContentType=WindowsRuntime] | Out-Null [Windows.Globalization.Language, Windows.Globalization, ContentType=WindowsRuntime] | Out-Null function Await-Result($AsyncOperation, [Type]$ResultType) { $method = [System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object { $_.Name -eq 'AsTask' -and $_.IsGenericMethod -and $_.GetParameters().Count -eq 1 } | Select-Object -First 1 $task = $method.MakeGenericMethod($ResultType).Invoke($null, @($AsyncOperation)) $task.Wait() return $task.Result } $file = Await-Result ([Windows.Storage.StorageFile]::GetFileFromPathAsync((Resolve-Path -LiteralPath $Path).Path)) ([Windows.Storage.StorageFile]) $stream = Await-Result ($file.OpenAsync([Windows.Storage.FileAccessMode]::Read)) ([Windows.Storage.Streams.IRandomAccessStream]) $decoder = Await-Result ([Windows.Graphics.Imaging.BitmapDecoder]::CreateAsync($stream)) ([Windows.Graphics.Imaging.BitmapDecoder]) $bitmap = Await-Result ($decoder.GetSoftwareBitmapAsync()) ([Windows.Graphics.Imaging.SoftwareBitmap]) $lang = [Windows.Globalization.Language]::new('ar-SA') $engine = [Windows.Media.Ocr.OcrEngine]::TryCreateFromLanguage($lang) if ($null -eq $engine) { throw 'Windows OCR engine unavailable' } $result = Await-Result ($engine.RecognizeAsync($bitmap)) ([Windows.Media.Ocr.OcrResult]) $result.Lines | ForEach-Object { $_.Text }