• Site for Newbie Windows Administrators
admincentric
  • Home
  • Active Directory
  • Hyper-V
  • PowerShell
  • Free Tools
  • Contact

PowerShell Switch Statement

Feb 18, 2024 Admin

In PowerShell, the switch statement is used for conditional branching based on the value of an expression. It allows you to compare a given value against multiple possible values and execute different code blocks depending on the match.

Syntax

Here's a detailed explanation of the switch statement in PowerShell:

switch ($expression) {
"value1" {
# Code to execute if $expression matches "value1"
}
"value2" {
# Code to execute if $expression matches "value2"
}
# Additional cases can be added as needed
default {
# Code to execute if none of the cases match
}
}

Syntax Explanation

  • $expression: This is the value that you want to compare against multiple cases.
  • Cases: Each block within the switch statement represents a case. It begins with a value (e.g., "value1") followed by a code block in curly braces {}. If the value of $expression matches any of the cases, the corresponding code block is executed.
  • default case: This is optional and provides a block of code to execute if none of the specified cases match the value of $expression.

Example 1 – Finding Out the Day of Week

This example passes the current day of the week to the PowerShell switch statement. Different responses are configured for each day of the week within the Switch statement. The one that matches the current day gets printed on the console.

$day = (Get-Date).DayOfWeek

switch ($day) {
“Monday” {
Write-Host “It’s the start of the week!”
}
“Tuesday” {
Write-Host “It’s Tuesday!”
}
“Wednesday” {
Write-Host “It’s the middle of the week!”
}
“Thursday” {
Write-Host “It’s almost Friday!”
}
“Friday” {
Write-Host “It’s finally Friday!”
}
Default {
Write-Host “It’s the weekend!”
}
}

The output should look something like this.

Example 2 – Grading Student Score

This example grades the provided score depending on different switch conditions that make use of -ge and -lt operators. The condition that covers the range within which the provided score falls gets executed.

$score = 85

switch ($score) {
{ $_ -ge 90 } {
Write-Host "Grade: A"
}
{ $_ -ge 80 -and $_ -lt 90 } {
Write-Host "Grade: B"
}
{ $_ -ge 70 -and $_ -lt 80 } {
Write-Host "Grade: C"
}
{ $_ -ge 60 -and $_ -lt 70 } {
Write-Host "Grade: D"
}
default {
Write-Host "Grade: F (Fail)"
}
}

The output should look something like this.

Example 3 – Checking Whether Windows Services are Working or Not

This example illustrates how the switch statement can be nested within a foreach statement. The below script checks whether a specific list of windows’ services is running or not. First, the list of services is looped through and each of the service being looped through is passed to the switch statement. Then the switch condition that matches service checks whether the service is running or not and prints the same on the console.

$servicesToCheck = "EventLog", "Wsearch", "RemoteRegistry"
foreach ($service in $servicesToCheck) {
switch ($service) {
"EventLog" {
$status = Get-Service -Name $service | Select-Object -ExpandProperty Status
Write-Host "$service service is $($status)"
}
"Wsearch" {
$status = Get-Service -Name $service | Select-Object -ExpandProperty Status
Write-Host "$service service is $($status)"
}
"RemoteRegistry" {
$status = Get-Service -Name $service | Select-Object -ExpandProperty Status
Write-Host "$service service is $($status)"
}
default {
Write-Host "Unknown service: $service"
}
}
}

The output should look something like this.

Home

Active Directory

Hyper-V

PowerShell

Free Tools

Contact

Trending News

How to enable Hyper-V in Windows? Coming Soon...

How to configure a Virtual Machine? Coming Soon...

Using PowerShell to manage a Virtual Machine Coming Soon...

How to install Active Directory on Windows Server 2016? Coming Soon...

How to create Active Directory Users? Coming Soon....

How to reset Active Directory User Passwords? Coming Soon...

How to unlock Active Directory User Accounts? Coming Soon...

Popular Resources

Popular resource 1 coming soon....coming soon

Popular resource 1 coming soon....coming soon

Popular resource 1 coming soon....coming soon

Popular resource 1 coming soon....coming soon

Table of Contents

Table of Contents

  • Syntax
  • Syntax Explanation
  • Example 1 – Finding Out the Day of Week
  • Example 2 – Grading Student Score
  • Example 3 – Checking Whether Windows Service is Running or Not

Connect with us!

Company
  • About admincentric
  • Advertise with us
  • Contact
Pages
  • Webinars
  • Deals Store
  • Privacy Policy
Deals
  • Hacking
  • Development
  • Android
RSS Feeds Contact Us

© admincentric, 2023. All Rights Reserved.