Switch statements in PowerShell are really useful to design a script with a menu, and to perform actions based on the selection.
Let's take a look at a few examples.
Example1 :
<#
PowerShell Switch Statement Example
Determine Day of the Week
#>
# User Input
$number = Read-Host "Enter a number from 1 to 7"
switch ($number) {
1 {
Write-Host "Day 1: Sunday"
}
2 {
Write-Host "Day 2: Monday"
}
3 {
Write-Host "Day 3: Tuesday"
}
4 {
Write-Host "Day 4: Wednesday"
}
5 {
Write-Host "Day 5: Thursday"
}
6 {
Write-Host "Day 6: Friday"
}
7 {
Write-Host "Day 7: Saturday"
}
default {
Write-Host "Invalid number. Please enter a number from 1 to 7."
}
}
Example 2:
# Menu Options
Write-Host "AI Operations Menu:"
Write-Host "1. Stable Diffusion"
Write-Host "2. AI Analysis"
Write-Host "3. Exit"
$choice = Read-Host "Enter Menu Option 1 - 3"
$choice = [int]$choice
switch ($choice) {
1 {
Write-Host "You selected: Stable Diffusion"
}
2 {
Write-Host "You selected: AI Analysis"
}
3 {
Write-Host "Exiting.."
}
default {
Write-Host "Invalid choice. Please enter a valid option (1, 2, or 3)."
}
}

-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Powershell,
- PowerShell: How to Check if a Module is Already Installed
- PowerShell: How to Write Output to a File
- PowerShell: Steps to Connect to connect to Exchange Online
- PowerShell Fix: Get-Help cannot find the Help files for this cmdlet on this computer
- PowerShell Traditional For Loop Example
- How to Run PowerShell Script as Administrator (Elevate)
- Update Powershell Using Command Line
- How to delete a file using PowerShell [Windows/macOS]
- PowerShell: Grep Command Alternative - Select-String
- PowerShell Concatenate String Examples
- How to add Sleep to PowerShell Script
- Connect Azure AD (Active Directory) for PowerShell
- How to Open PowerShell on Mac?
- List of PowerShell Function Commands for Mac
- How to Connect to Azure AD Account using PowerShell on Mac
- Download Google Chrome setup exe file using PowerShell
- Set Environment Variable in PowerShell for Mac
- How to switch to Powershell on Mac Terminal
- How to add sleep in Powershell Script
- PowerShell on Mac: The term get-service is not recognized as a name of a cmdlet, function, script file, or executable program
- How to Identify installed PowerShell version
- How to check PowerShell version
- How to install PowerShell on Mac using Brew
- PowerShell Switch Statement with Examples
- PowerShell ISE Alternative for Mac
More Posts:
- Take Screenshot on Mac OS X (Keyboard Shortcuts) - Mac-OS-X
- How to Remove All Terminated Console tabs at once in Eclipse - Eclipse
- What is list[-1] in Python Programming - Python
- How to Send Email using Java - Java
- How to List All Users in Linux - Linux
- Meaning of javascript:void(0) explained with example - JavaScript
- Fix: bash: syntax error near unexpected token - Bash
- Change SharePoint Online List Experience to New Experience from Classic - SharePoint