Convert all of your old images to WebP with Powershell.

Brandon Osborne
3 min readAug 17, 2020

--

I wanted a quick and efficient way to convert all of my site’s images into WebP format, but couldn’t find anything for Windows that tickled my fancy, so I wrote a quick script to do the heavy lifting for me.

The first thing that you’ll want to do is download the WebP library from Google. As of the time of writing, the latest version is 1.1.0 and that can be found here. The lib is pre-compiled, so you don’t have to do anything but download and extract the files from the archive.

The function that I wrote is pretty straight-forward. Here is a brief explanation of the command itself & it’s input parameters:

Convert-Images <input_directory> <output directory> <quality (0–100)>

The input directory is the source directory of your images stored in a non-next-gen format. The script will traverse through all of the sub-directories.

The output directory is where you want to put the images. The sub-directory that the script entered while traversing the structure will be appended to the end of the output directory.

Lastly, we have a simple quality value ranging from 0 to 100. The lower the quality the smaller the converted file will be, but the quality will also be lowered.

For my purposes, I used the following command:

Convert-Images “D:/DeleteMe/webp/Originals/” “D:/Projects/Web/Asp.Net/MVC/Umbraco/coderPro.Web.Corp.UI/media” 85

Last, but certainly not least, here is the script:

function Convert-Images {
param (
[string]$inputDirectory,
[string]$outputDirectory,
$quality
)
$files = Get-ChildItem $inputDirectory foreach ($file in $files) {
if($file -is [System.IO.DirectoryInfo]) {
if(-not (Test-Path -Path "$outputDirectory$file" -PathType Container)) {
try {
New-Item -Path $outputDirectory$file -ItemType Directory -ErrorAction Stop | Out-Null #-Force
}
catch {
Write-Error -Message "Unable to create directory '$outputDirectory$file'. Error was: $_" -ErrorAction Stop
}

"Successfully created directory '$outputDirectory$file'."
}
Convert-Images $file.FullName "$outputDirectory$file/" $quality
}
else {
if([System.Web.MimeMapping]::GetMimeMapping($file) -like 'image*') {
Start-Process -NoNewWindow -FilePath "D:\Utilities\libwebp-1.1.0-windows-x64\bin\cwebp.exe" -ArgumentList "$($file.FullName) -lossless -q $quality -o $outputDirectory$($file.BaseName).webp"
}
}
}

}
Convert-Images "D:/DeleteMe/webp/Originals/" "D:/Projects/Web/Asp.Net/MVC/Umbraco/coderPro.Web.Corp.UI/media" 85

You may view the code with much better formatting here: https://coderpro.net/blog/convert-all-of-your-old-images-to-next-gen-webp/

Voila! Just as simple as that all of your old files are converted to WebP and Google PageSpeed Insights will finally shut up. 😁

If you have any questions or need any help with your programming project, please feel free to drop me a line anytime, by using our contact form.

Until next time: Happy Coding!

Happy Coding

--

--

Brandon Osborne

I’m the Chief Software Architect of coderPro.net. I’ve been developing software on the Microsoft stack for 20 years & have been traveling the world for over 10!