Posts‎ > ‎

Powershell code to convert PNG files in the Screenshot folder to JPG files

posted Nov 28, 2018, 4:48 AM by Bill Hung
Bill Hung 2018-11-28 Nagoya

function Convert-PNG-to-JPG{
  [cmdletbinding()]
  param([Parameter(Mandatory=$true, ValueFromPipeline=$true)] $Path)

  process{
    if ($Path -is [string])
      {$Path = get-childitem $Path}
    
    $Path | foreach {
      $image = [System.Drawing.Image]::FromFile($($_.FullName));
      $FilePath = [IO.Path]::ChangeExtension($_.FullName, '.jpg');
      $image.Save($FilePath, [System.Drawing.Imaging.ImageFormat]::Jpeg);
      $image.Dispose();
    }
  }
}

cd C:\Users\Bill\Pictures\Screenshots

Get-ChildItem *.png | Convert-PNG-to-JPG
Comments