Create a multiple-choice quiz using PowerShell

Tomorrow I’m taking the AZ-800 (Administering Windows Server Hybrid Core Infrastructure) exam, and I wanted to create a simple test exam quiz for myself with multiple-choice questions. This blog post will show you how to do that using an Excel file with questions and A, B, C, and D answers, including the correct answer to check on.

How the script works

When starting the Start-Quiz script, you must specify the path to the Excel file containing the questions and answers. This Excel file must include the following columns with the correct answer in the last column (CorrectAnswer):

The script reads the Excel file and will show you the question and the four answers, which you can choose by pressing A, B, C, or D on your keyboard. After pressing Enter, it will tell you if it’s correct and continue to the next question. When all questions are answered, it will tell you how many you answered correctly or incorrectly.

Running the script

You can execute the script by running “.\Start-Quiz.ps1” without parameters, which will automatically ask you for the full path to the Excel file containing the questions and answers. You can also use these parameters:

  • The -FilePath parameter can be used to specify the Excel file, for example: -FilePath d:\temp\testquestions.xlsx
  • The -SkipImportExcelModuleCheck parameter can be used to skip checking if the ImportExcel module is installed. It takes a little while to detect if it’s installed and will install it if not. If you already have it installed, you can skip this check by using this parameter.

In the example below, I start the script with both parameters:

.\Start-Quiz.ps1 -FilePath D:\temp\testquestions.xlsx -SkipImportExcelModuleCheck

This will look like this on your console screen: (It will clear your screen after each question using Clear-Host)

Specified file D:\temp\testquestions.xlsx found, continuing...
2 Questions imported...
Starting quiz...
Press Enter to continue...:

Question 1 of 2
What is PowerShell?

Answer A: Something from the Turtles
Answer B: Something electricians use
Answer C: Scripting language
Answer D: A phone case

Type A,B,C or D to answer the question
Correct! C is the correct answer
Press Enter to continue...: 

Question 2 of 2
What is not a Microsoft product?

Answer A: Word
Answer B: An Access Point
Answer C: Excel
Answer D: PowerPoint

Type A,B,C or D to answer the question
Correct! B is the correct answer
Press Enter to continue...: 

Your results are:

2 correctly answered
0 incorrectly answered

The script

Below are the contents of the script, save it to c:\scripts, for example and run it with (or without) the parameters as described above:

param (
    [Parameter(Mandatory = $true, HelpMessage = "Enter the complete path to the .xlsx")][string]$FilePath,
    [Parameter(Mandatory = $false, HelpMessage = "Skip the ImportExcel module check")][switch]$SkipImportExcelModuleCheck
#Validate complete path and extension of the file
if (-not (Test-Path $FilePath)) {
    Write-Warning ("Specified file {0} can't be found or accessed, check path and permissions...." -f $FilePath)
else {
    Write-Host ("Specified file {0} found, continuing..." -f $FilePath) -ForegroundColor Green

if (-not ($FilePath.EndsWith('.xlsx'))) {
    Write-Warning ("Specified file {0} has no .xlsx extension, exiting..." -f $FilePath)

#Check if ImportExcel module is installed if not skipped by NoImportExcelModuleCheck parameter
if (-not ($SkipImportExcelModuleCheck)) {
    Write-Host ("Checking if ImportExcel module is installed...") -ForegroundColor Green
    if (-not (Get-Module -ListAvailable | Where-Object Name -Match 'ImportExcel')) {
        Write-Warning ("Required ImportExcel module is not installed, installing now...")
        Install-Module -Name ImportExcel -SkipPublisherCheck:$true -Force:$true

#Read questions into $questions variable
$questions = Import-Excel -Path $FilePath -ErrorAction SilentlyContinue
if ($questions.question -and $questions.CorrectAnswer) {
    Write-Host ("{0} Questions imported..." -f $questions.Count) -ForegroundColor Green
else {
    Write-Warning ("Error importing {0}, check the format in the file or if ImportExcel module is installed. Exiting..." -f $FilePath)
#Start quiz
$goodanswers = 0
$badanswers = 0
$totalquestions = $questions.Count
$currentquestionnumber = 1
Write-Host ("Starting quiz...") -ForegroundColor Green

#Loop through questions from the Excel file and show the score when done
foreach ($question in $questions) {
    Write-Host ("Question {0} of {1}" -f $currentquestionnumber, $totalquestions)
    Write-Host ("$($question.question)")
    Write-Host ("`n")
    Write-Host ("Answer A: {0}" -f $question.AnswerA)
    Write-Host ("Answer B: {0}" -f $question.AnswerB)
    Write-Host ("Answer C: {0}" -f $question.AnswerC)
    Write-Host ("Answer D: {0}" -f $question.AnswerD)
    Write-Host ("`n")
    Write-Host ("Type A,B,C or D to answer the question")

    #Wait until a,b,c or d is pressed
    do {
        $key = [Console]::ReadKey($true)
        $value = $key.KeyChar
        switch ($value) {
            a { $answer = 'a' }
            b { $answer = 'b' }
            c { $answer = 'c' }
            d { $answer = 'd' }
    while ($value -notmatch 'a|b|c|d')
    #Check if the answer is correct
    if ($answer -eq $question.CorrectAnswer) {
        Write-Host ("Correct! {0} is the correct answer`n" -f $question.CorrectAnswer) -ForegroundColor Green -NoNewline
    else {
        Write-Warning ("Incorrect! {0} is the correct answer`n" -f $question.CorrectAnswer)
#Display totals
Write-Host ("Your results are:`n`n{0} correctly answered`n{1} incorrectly answered`n`n" -f $goodanswers, $badanswers) -ForegroundColor Green

Download the script(s) from GitHub here (Including a sample Excel file)

4 thoughts on “Create a multiple-choice quiz using PowerShell

  1. Good luck on this one! I was thinking about taking something like this, we are primarily all On-Prem so figured this one would be the best of both worlds I suppose.

    • Many of our customers are in a hybrid situation and this was a nice new certification for it 🙂 Learning for the first exam, there weren’t that many things that I didn’t know already, and I was curious how the second exam (AZ-801) will be… Tomorrow morning I will take the examen at 10:15, fingers crossed 😉

  2. Actually not a bad idea, I tinkered a bit with it and changed it to turn my obsidian (Markdown) notes into multiple choice / user input questions. Not very professionell, but a very fast way to recycle notes into something more useful. I will upload it to GitHub and link to the original. THX

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.