If you’re a LEGO enthusiast like me, you’ve probably found yourself manually downloading building instructions from LEGO’s website. While it’s fun to browse, it gets tedious when you’re trying to archive or organize multiple sets. So I wrote a PowerShell function to automate the process.

While I was working on this little project I saw a couple of hurdles I needed to overcome. First, the ID of the set was not the same as the ID of the PDF file I needed to download. Second, every page had also PDF files that were not directly related to the set. And third, how do I want to store them.

With all these hurdles I wrote a script that downloads the instructions to a folder with the ID of the set.

The Code

Function Get-LegoInstructions {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        [string]$SetID
    )

    # Stap 1: Get the downloadpage
    $instructionsUrl = "https://www.lego.com/en-us/service/building-instructions/$SetID"
    $html = Invoke-WebRequest -Uri $instructionsUrl -UseBasicParsing

    # Stap 2: Filter PDF-links
    $pdfLinks = $html.Links | Where-Object { $_.href -match "\d{5,}\.pdf$" }

    if ($pdfLinks.Count -eq 0) {
        Write-Host "No valid PDF instructions found for set $SetID."
        return
    }

    # Stap 3: Create folder
    $downloadFolder = Join-Path $env:USERPROFILE "Downloads\LegoInstructions$SetID"
    if (-not (Test-Path $downloadFolder)) {
        New-Item -Path $downloadFolder -ItemType Directory | Out-Null
    }

    # Stap 4: Download
    $i = 1
    foreach ($link in $pdfLinks) {
        $pdfUrl = $link.href
        $filename = if ($pdfLinks.Count -gt 1) { "$SetID-part$i.pdf" } else { "$SetID.pdf" }
        $filePath = Join-Path $downloadFolder $filename

        Write-Verbose "Download: $pdfUrl"
        Invoke-WebRequest -Uri $pdfUrl -OutFile $filePath
        Write-Verbose "Saved as: $filePath"
        $i++
    }

    Write-Host "Finished downlaoding instructions for Lego set: $SetID."
}

Get-LegoInstructions -SetID $SetID -Verbose 
## 21061 for "Architecture | Notre-Dame de Paris"
## This will download the instructions for set 21061 into your Downloads\LegoInstructions061 folder.

How It Works – Behind the Scenes

Let me walk you through what happens when you run the Get-LegoInstructions function. Think of it as a little digital scavenger hunt, where PowerShell plays the role of your loyal assistant.

Step 1: Scouting the Territory

The journey begins with a simple URL: https://www.lego.com/en-us/service/building-instructions/{SetID}

PowerShell uses Invoke-WebRequest to visit this page, just like a browser would. It quietly grabs the HTML content, which is essentially the blueprint of the page. This is where all the hidden treasures, like download links, are buried.

Step 2: Finding the Treasure

Now comes the clever part. The script scans through all the hyperlinks on the page, looking for ones that match a very specific pattern:
1234567.pdf

This is done using a regular expression (\d{5,}\.pdf$) that filters out everything except valid LEGO instruction PDFs.

Step 3: Preparing the Vault

Once the PDFs are found, PowerShell creates a dedicated folder for the set inside your Downloads\LegoInstructions directory. This keeps things tidy and makes sure each set has its own little home.

Step 4: The Heist

With everything ready, PowerShell starts downloading each PDF file. If there are multiple parts, it names them accordingly (SetID-part1.pdfSetID-part2.pdf, etc.). Throughout the process, it whispers updates using Write-Verbose, so you can follow along if you run the script with -Verbose.

Final Touch

When the last file is saved, PowerShell proudly announces:
“Finished downloading instructions for LEGO set: {SetID}.”

— Mission accomplished. —

Ideas for Improvement

  • Automatically fetch the set name and use it as the folder name.
  • Add support for downloading multiple sets in one go.
  • Include error handling for network issues or invalid set IDs.

Wrapping It Up

With just a few lines of PowerShell, we’ve turned a manual chore into a smooth, automated process. Whether you’re a LEGO collector archiving your sets, a parent organizing instructions for your kids, or just a fan of scripting for fun, this function saves time and adds a touch of elegance to your workflow.

But this is just the beginning. PowerShell gives us the tools to go further: imagine fetching set names automatically, downloading instructions for multiple sets in one go, or even integrating with a GUI for non-technical users.

Automation doesn’t have to be complex, it just has to be useful. And sometimes, it can even be fun.

Leave a Reply

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