Featured resource
2025 Tech Upskilling Playbook
Tech Upskilling Playbook

Build future-ready tech teams and hit key business milestones with seven proven plays from industry leaders.

Check it out
  • Lab
    • Libraries: If you want this lab, consider one of these libraries.
    • Cloud
Google Cloud Platform icon
Labs

Logic Flow for PowerShell Core in Linux

PowerShell is no different than other computer languages in that more complex capabilities require the addition of logic. For those familiar with logic controls in other programming languages, they'll find no surprises. In this hands-on lab, we cover some of the PowerShell specifics for controlling logic flow, conditional statements, comparison operators, and switches.

Google Cloud Platform icon
Lab platform
Lab Info
Level
Intermediate
Last updated
Apr 06, 2025
Duration
1h 0m

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
Table of Contents
  1. Challenge

    Perform a System Update, Register the MS RedHat Repository, and Install PowerShell
    1. Use the yum command to sync the package index files from their sources via the Internet.
    sudo yum check-update
    
    1. Use the yum command to install the newest versions of all installed packages on CentOS.
    sudo yum update
    
    1. Register the Microsoft RedHat repository.
    curl https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo
    
    1. Install PowerShell.
    sudo yum install -y powershell
    
    1. Start PowerShell.
    pwsh
    
  2. Challenge

    Work with PowerShell Conditional Statements
    1. Set variable a equal to 3.
    $a=3
    
    1. Create a new file name script1.ps1 using vi.

    2. Input the following into the script1.ps1 file:

    if ($a -gt 2) {
        Write-Host "The value $a is greater than 2."
    }
    
    1. Save the script1.ps1 file in vi using :wq.

    2. Run the script1.ps1 file.

    ./script1.ps1
    
    1. Set the variable a equal to 1.
    $a=1
    
    1. Run script1.ps1 again.
    ./script1.ps1
    
    1. Create a new script using vi called script2.ps1.

    2. Input the following into the script2.ps1 file:

    if ($a -gt 2) {
        Write-Host "The value $a is greater than 2."
    }
    else {
        Write-Host ("The value $a is less than or equal to 2,")
    }
    
    1. Run the script2.ps1 file.
    ./script2.ps1
    
    1. Change the variable a back to 3.
    $a=3
    
    1. Run the script2.ps1 file again.
    ./script2.ps1
    
  3. Challenge

    Work with PowerShell Switches to Handle Multiple If Statements
    1. Enter the following at the PowerShell prompt:
    switch (3)
    {
        1 {"It is one."}
        2 {"It is two."}
        3 {"It is three."}
        4 {"It is four."}
    }
    
    1. Enter the following at the PowerShell prompt:
    switch (3)
    {
        1 {"It is one."}
        2 {"It is two."}
        3 {"It is three."}
        4 {"It is four."}
        3 {"Three again."}
    }
    
    1. Enter the following at the PowerShell prompt:
    switch (3)
    {
        1 {"It is one."}
        2 {"It is two."}
        3 {"It is three."; Break}
        4 {"It is four."}
        3 {"Three again."}
    }
    
    1. Enter the following at the PowerShell prompt:
    switch (4, 2)
    {
        1 {"It is one." }
        2 {"It is two." }
        3 {"It is three." }
        4 {"It is four." }
        3 {"Three again."}
    }
    
    1. Enter the following at the PowerShell prompt:
    switch (4, 2)
    {
        1 {"It is one."; Break}
        2 {"It is two." ; Break }
        3 {"It is three." ; Break }
        4 {"It is four." ; Break }
        3 {"Three again."}
    }
    
    1. Exit PowerShell.
    exit
    
About the author

Pluralsight Skills gives leaders confidence they have the skills needed to execute technology strategy. Technology teams can benchmark expertise across roles, speed up release cycles and build reliable, secure products. By leveraging our expert content, skill assessments and one-of-a-kind analytics, keep up with the pace of change, put the right people on the right projects and boost productivity. It's the most effective path to developing tech skills at scale.

Real skill practice before real-world application

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Learn by doing

Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.

Follow your guide

All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.

Turn time into mastery

On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.

Get started with Pluralsight