# Batch save images into a new format via PowerShell

G18th December 2015

Use a PowerShell script to copy all images in a directory and save them into a different image file format.

I recently had a directory full of JPG images which I needed to convert into PNG format. Whilst I could open Adobe Photoshop and process the images one-by-one, batch-processing using PowerShell seemed like it would be more efficient.

To achieve my goal I used the following script, which is adapted from The Scripting Guy:

function ConvertImage{
param ([string]$path)$path="c:\path\to\files" #path to files
if (Test-Path $path) { #Load required assemblies and get object reference [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null foreach($file in (ls "$path\*.jpg")){$convertfile = new-object System.Drawing.Bitmap($file.Fullname)$newfilname = ($file.Fullname -replace '([^.]).jpg','$1') + ".png"
$convertfile.Save($newfilname, "png")
$file.Fullname } } else { Write-Host "Path not found." } };ConvertImage -path$args[0]


Before execution, the script needs to be customised via the following steps:

## Step 4: Define the file extension

Adjust the code to reflect the change in file extension from .jpg to .png (or whichever the target format is)

$convertfile.Save($newfilname, "png")

## Step 5: Execute the script

The new images will progressively appear in the same directory as the old.