MicrosoftPowershellTips & TricksWindows 10

How to Convert PNG to JPG in Bulk Using PowerShell

Introduction

If you need to convert a substantial number of PNG images to the JPG format, doing so manually can be incredibly tedious. PowerShell offers a powerful solution to automate this process, saving you valuable time and effort. In this article, we’ll guide you through the steps of bulk converting PNG images to JPG using a simple PowerShell script.

Prerequisites

Why Convert PNG to JPG?

  • Here are some compelling reasons to convert PNG images to JPG:

    • File Size Reduction: JPG uses lossy compression, meaning it slightly reduces image quality to achieve smaller file sizes. This can save significant storage space, especially for web use.
    • Web Compatibility: JPG is a very widely supported format, ensuring compatibility across nearly all browsers and image viewing applications.
    • Faster Loading Times: Smaller JPG file sizes result in faster image loading times on websites.
Recommended For You:  Generating a Hyper-V VM Report: A PowerShell Scripting Guide

Steps for Bulk Converting PNG to JPG using PowerShell

  1. Open PowerShell: Find PowerShell in the Start menu (it usually comes pre-installed on Windows), right-click, and choose “Run as administrator”.

  2. Write and Execute Your PowerShell Script: Create or open a PowerShell script file (e.g., convert_png_to_jpg.ps1) and paste the following code:

# Specify the directory containing your PNG files
$sourceDirectory = "C:\Your\PNG\Folder\Path"

# Get all PNG files in the specified directory
$pngFiles = Get-ChildItem -Path $sourceDirectory -Filter "*.png"

# Load the necessary .NET assembly
Add-Type -AssemblyName System.Drawing

foreach ($pngFile in $pngFiles) {
    $image = [System.Drawing.Image]::FromFile($pngFile.FullName)
    $jpgPath = $pngFile.FullName.Replace(".png", ".jpg")
    $image.Save($jpgPath, [System.Drawing.Imaging.ImageFormat]::Jpeg)

    # Dispose of the original image object (optional)
    $image.Dispose()
}

Code Explanation:

  • Line 1: Sets the path to the directory containing your PNG files. Replace "C:\Your\PNG\Folder\Path" with the actual path.
  • Line 3: Retrieves all files with the “.png” extension within the specified directory.
  • Line 5: Loads the .NET assembly needed for image manipulation.
  • Line 7 (Inside loop): Opens the current PNG file as a System.Drawing.Image object.
  • Line 8 (Inside loop): Constructs the output JPG filename by replacing the “.png” extension.
  • Line 9 (Inside loop): Saves the image in JPG format using the specified quality.
  • Line 11 (Inside loop): (Optional) Releases the image object to free up memory.
Recommended For You:  Mastering Hyper-V Storage QoS Configuration with PowerShell

Run the PowerShell Script:

  • In the PowerShell window, navigate to the directory where you saved your script, then type the script’s name and extension:
.\convert_png_to_jpg.ps1

Important Notes:

Muhammad Faizan

Hi, My name is Muhammad Faizan and i have spent last 15 years working as System Administrator mainly with Microsoft Technologies. I am MCSE, MCTP, MCITP, certified professional. I love scripting and Powershell is the scripting language i am in love with.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Close
Back to top button