Mike Wilcock - WebHelp
PowerShell NASA Astronomy Picture of the Day
Example Code > PowerShell Scripts > PowerShell NASA Astronomy Picture of the Day

The following example PowerShell script was created to demonstrate the usage of the NASA Astronomy Picture of the Day API (APOD).

The code uses the PowerShell Invoke-Webrequest cmdlet to retrieve data from the the NASA API and then outputs the resultant image or video information to a formatted html text file which is stored locally on the users disk drive.

At some point in the future, I may add the option to add a drop-down date selection component so that you can select images from previous days, but for the time being it is a working example.

#----------------------------------------------------------------------------#
# Program Copyright    : Mike Wilcock.
# Program Name         : nasa_apod.ps1.
#----------------------------------------------------------------------------#
# Program Created      : 1st March 2025.
# Program Code Type    : PowerShell Script (version 5.1.22621.436).
# Author               : Michael Wilcock, IT Technician.
#----------------------------------------------------------------------------#
# Version              : 1.51
#----------------------------------------------------------------------------#
# Purpose              : Query the NASA Astronomy Picture of the Day (APOD) API
#                        and write the results as an html document.
#----------------------------------------------------------------------------#

Add-Type -AssemblyName PresentationCore,PresentationFramework

# Store API key.
$apiKey = "DEMO_KEY"

# Store date and time.  Used in filename.
$cDateTime = Get-Date -Format "dd-MM-yyyy-HH-mm-ss"

# Output to console.
Write-Host("Getting APOD Data - Please Wait")

# Make web request.
try
{
    # Youtube video.
    #$imgInfo = Invoke-WebRequest -Uri "https://api.nasa.gov/planetary/apod?api_key=$apiKey&date=2025-01-05&thumbs=true" | ConvertFrom-Json
   
    # Bespoke video without thumbnail.
    #$imgInfo = Invoke-WebRequest -Uri "https://api.nasa.gov/planetary/apod?api_key=$apiKey&date=2025-03-02&thumbs=true" | ConvertFrom-Json
        
    # Image / video of the day.
    $imgInfo = Invoke-WebRequest -Uri "https://api.nasa.gov/planetary/apod?api_key=$apiKey&thumbs=true" | ConvertFrom-Json
}
# Error.
catch
{
    [System.Windows.MessageBox]::Show("There was an error accessing the NASA Astronomy Picture of the Day API`n`n" + $_.Exception.Message + "`n`nThe program will now terminate.", "Error", "Ok", "error")
    return
}
# Readability.
$cTitle = $imgInfo.title
$cExplanation = $imgInfo.explanation
$cHdUrl = $imgInfo.hdurl
$cUrl = $imgInfo.url
$cThumbnailUrl = $imgInfo.thumbnail_url
$cDate = ([DateTime]$imgInfo.date).ToString("dd\/MM\/yyyy") # British date.
        
# Create simple html document for iamges.
if($imgInfo.media_type -eq "image")
{
    # Retrieve filemane only.
    $imgName = $cHdUrl.Substring($cHdUrl.LastIndexOf("/") + 1)
    # Output to console.
    Write-Host("Saving Image File - Please Wait")
    # Download image and save to disk.
    Invoke-WebRequest -Uri $cHdUrl -OutFile ($PSScriptRoot + "\$imgName")
    # Create image hyperlink.
    $cHyperlink = "<a href='$imgName' target='blank'><img src='$imgName' alt='$cTitle' title='$cTitle' width=650px></a>"
   
    $cHTML = @"
<!DOCTYPE html>
<html>
<head>
    <title>NASA Astronomy Picture of the Day</title>
</head>
<body>
    <h1 style="text-align: center;">NASA Astronomy Picture of the Day</h1>
   
    <div id="image" style="width:100%; text-align: center;">
        $cHyperlink
        <p>click thumbnail to view image in full size</p>
    </div>
   
    <div id="image_info" style="width:100%; text-align: center;">
        <h2>$cTitle</h2>
        <p>$cDate - [dd/mm/yyyy]</p>
        <p>$cExplanation</p>
    </div>
</body>
</html>
"@
}
# Create simple html document for videos.
else
{
    # Retrieve filemane only.
    $imgName = $cUrl.Substring($cUrl.LastIndexOf("/") + 1)
    # We don't have a thumbnail image.
    if($null -eq $cThumbnailUrl)
    {
        # Create video hyperlink.
        $cHyperlink = "<a href='$cUrl' target='blank' title='$cTitle'>$cUrl</a>"
    }
    # We do have a thumbnail image.
    else
    {
        # Create video hyperlink.
        $cHyperlink = "<a href='$cUrl' target='blank'><img src='$cThumbnailUrl' alt='$cTitle' title='$cTitle' width=650px></a>"
    }
    $cHTML = @"
<!DOCTYPE html>
<html>
<head>
    <title>NASA Astronomy Picture of the Day</title>
</head>
<body>
    <h1 style="text-align: center;">NASA Astronomy Picture of the Day</h1>
   
    <div id="image" style="width:100%; text-align: center;">
        $cHyperlink
        <p>click hyperlink to view video in full size</p>
    </div>
   
    <div id="image_info" style="width:100%; text-align: center;">
        <h2>$cTitle</h2>
        <p>$cDate - [dd/mm/yyyy]</p>
        <p>$cExplanation</p>
    </div>
</body>
</html>
"@
}
# Output to console.
Write-Host("Creating HTML Document - Please Wait")
# Write HTML data to disk.
Set-Content -Path ($PSScriptRoot + "\nasa_apod_" + $cDateTime + ".html") -Value $cHTML
# Prompt user to view the HTML file.
$n = [System.Windows.MessageBox]::Show("Would you like to view the NASA Astronomy Picture of the Day in your Web Browser - [Y/N]", "View in Browser", "YesNo", "question")
# The man from Del Monte, he say yes.
if($n -eq "yes")
{
    # Launch default html app.
    Invoke-Item ($PSScriptRoot + "\nasa_apod_" + $cDateTime + ".html")
}

See Also