Logic Flow for PowerShell Core in Linux

1 hour
  • 3 Learning Objectives

About this Hands-on Lab

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.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

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
  2. Use the yum command to install the newest versions of all installed packages on CentOS.

    sudo yum update
  3. Register the Microsoft RedHat repository.

    curl https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo
  4. Install PowerShell.

    sudo yum install -y powershell
  5. Start PowerShell.

    pwsh
Work with PowerShell Conditional Statements
  1. Set variable a equal to 3.

    $a=3
  2. Create a new file name script1.ps1 using vi.

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

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

  5. Run the script1.ps1 file.

    ./script1.ps1
  6. Set the variable a equal to 1.

    $a=1
  7. Run script1.ps1 again.

    ./script1.ps1
  8. Create a new script using vi called script2.ps1.

  9. 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,")
    }
  10. Run the script2.ps1 file.

    ./script2.ps1
  11. Change the variable a back to 3.

    $a=3
  12. Run the script2.ps1 file again.

    ./script2.ps1
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."}
    }
  2. 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."}
    }
  3. 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."}
    }
  4. 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."}
    }
  5. 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."}
    }
  6. Exit PowerShell.

    exit

Additional Resources

In order to get used to controlling PowerShell logic flow, we open up a terminal session, practice using conditional statements, switches, and loops in PowerShell for Linux.

What are Hands-on Labs

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.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?