Install GIMP Using PowerShell

Problem

In this article, I will show you how to install GIMP using PowerShell.

The installation process consists of three parts:

  1. Define download URL and installer location
  2. Download GIMP installer
  3. Install GIMP silently

You can directly apply the third step above if you already have the installer or download it manually. All solutions below share all the steps above. They only differ in how the installer is downloaded.

Using Invoke-WebRequest and Start-Process

First we need to define variables for download URL and installer location. Then, we use Invoke-WebRequest cmdlet to download the file and put it in the folder.

Next, we perform silent installation using Start-Process cmdlet. We add -Wait parameter to wait until installation process completely finished.

For installation process, we add /VERYSILENT parameter to prevent the wizard and the background window to be displayed. /ALLUSERS parameter is used to install GIMP for all users of the computer and /NORESTART parameter will prevent the system to restart after installation process finished.

Last, we will remove the installer using Remove-Item cmdlet.


# Define download URL and installer location
$gimpUrl = "https://download.gimp.org/gimp/v2.10/windows/gimp-2.10.36-setup.exe"
$installer = "$env:TEMP\gimp-2.10.36-setup.exe"

# Download GIMP installer
Invoke-WebRequest -Uri $gimpUrl -OutFile $installer

# Install GIMP silently
Start-Process -FilePath $installer -ArgumentList /VERYSILENT, /ALLUSERS, /NORESTART, /LOG="$env:TEMP\GIMP-Install.log" -Wait

# Remove installer
Remove-Item $installer

Using Invoke-RestMethod and Start-Process

Similar to previous solution, but in this one we will use Invoke-RestMethod cmdlet to download the file. We can use this cmdlet because the communication is using HTTP(S) that is supported by RESTful services.


# Define download URL and installer location
$gimpUrl = "https://download.gimp.org/gimp/v2.10/windows/gimp-2.10.36-setup.exe"
$installer = "$env:TEMP\gimp-2.10.36-setup.exe"

# Download GIMP installer
Invoke-RestMethod -Uri $gimpUrl -OutFile $installer

# Install GIMP silently
Start-Process -FilePath $installer -ArgumentList /VERYSILENT, /ALLUSERS, /NORESTART, /LOG="$env:TEMP\GIMP-Install.log" -Wait

# Remove installer
Remove-Item $installer

Using WebClient and Start-Process

We can also use WebClient class from .NET Framework to download the file since PowerShell is deeply integrated with .NET Framework.


# Define download URL and installer location
$gimpUrl = "https://download.gimp.org/gimp/v2.10/windows/gimp-2.10.36-setup.exe"
$installer = "$env:TEMP\gimp-2.10.36-setup.exe"

# Download GIMP installer
$WebClient = New-Object System.Net.WebClient
$webclient.DownloadFile($gimpUrl, $installer)

# Install GIMP silently
Start-Process -FilePath $installer -ArgumentList /VERYSILENT, /ALLUSERS, /NORESTART, /LOG="$env:TEMP\GIMP-Install.log" -Wait

# Remove installer
Remove-Item $installer

Using HttpClient and Start-Process

We can also use HttpClient from .NET Framework to download the installer, then install GIMP silently.

There are two flavors in using .NET Framework C# code from PowerShell. We can create the object using PowerShell syntax as in previous solution or we can also embed C# code in our script then invoke it.

In this case, we present both of the approaches.

Create C# object using PowerShell syntax

In this approach, we need to apply dispose pattern by enclosing each call to HttpClient constructor, GetStreamAsync and FileStream methods with try-finally block. It is optional to put catch block if we want to handle the exception specifically.

At finally block we close the connection after we have used disposable object. Otherwise, we won’t be able to perform silent installation because the process is still being used by another process.

A more concise way to apply dispose pattern in C# is to use using statement, but since PowerShell doesn’t have the syntax for the pattern we can use try-finally block as the alternative.


using namespace System.IO
using namespace System.Net.Http

# Define download URL and installer location
$gimpUrl = "https://download.gimp.org/gimp/v2.10/windows/gimp-2.10.36-setup.exe"
$installer = "$env:TEMP\gimp-2.10.36-setup.exe"

# Download GIMP installer
try {
    $client = [HttpClient]::new()    
    try {
        $stream = $client.GetStreamAsync($gimpUrl)
        try {
            $fileStream = [FileStream]::new($installer, [FileMode]::Create)
            $stream.GetAwaiter().GetResult().CopyTo($fileStream)       
        }
        finally {
            <#Do this after the try block regardless of whether an exception occurred or not#>
            $fileStream.Dispose()
        }         
    }
    finally {
        <#Do this after the try block regardless of whether an exception occurred or not#>
        $stream.Dispose()
    }    
}
finally {
    <#Do this after the try block regardless of whether an exception occurred or not#>
    $client.Dispose()
}

# Install GIMP silently
Start-Process -FilePath $installer -ArgumentList /VERYSILENT, /ALLUSERS, /NORESTART, /LOG="$env:TEMP\GIMP-Install.log" -Wait

# Remove installer
Remove-Item $installer

Embed C# code then invoke it from PowerShell

In this approach, we copy and paste our C# code to PowerShell, then enclose it with Add-Type -TypeDefinition in order to add it to PowerShell session. Then, we will be able to call the method to download the file from PowerShell.


# Define download URL and installer location
$gimpUrl = "https://download.gimp.org/gimp/v2.10/windows/gimp-2.10.36-setup.exe"
$installer = "$env:TEMP\gimp-2.10.36-setup.exe"

# Create C# Code to Download GIMP installer
Add-Type -TypeDefinition @"
    using System.IO;
    using System.Net.Http;

    public static class Downloader {
        public static void DownloadInstaller() {

            using (var client = new HttpClient())
            {
                using (var s = client.GetStreamAsync(@"$gimpUrl"))
                {
                    using (var fs = new FileStream(@"$installer", FileMode.OpenOrCreate))
                    {
                        s.Result.CopyTo(fs);
                    }
                }
            }
        }
    }
"@
[Downloader]::DownloadInstaller()

# Install GIMP silently
Start-Process -FilePath $installer -ArgumentList /VERYSILENT, /ALLUSERS, /NORESTART, /LOG="$env:TEMP\GIMP-Install.log" -Wait

# Remove installer
Remove-Item $installer

Conclusion

To install GIMP silently in PowerShell, we could download the file first and then perform silent installation. To download the file, there are some cmdlets we can use: using Invoke-WebRequest or Invoke-RestMethod. We can also use .NET Framework class like WebClient or HttpClient to download the installer.

If we already have the installer, we can perform silent installation directly. After installation process finished, we remove the installer using Remove-Item cmdlet.