Learn

Let’s take a look at what the components of a function are, and how to call it.

The components of a function in PowerShell are:

  • the keyword Function
  • a descriptive function name
  • a code body inside brackets
Function greet { Write-Host "Hello, there!" }

This example from the last exercise starts with the keyword function, followed by a name that describes the function action. Lastly, the code block inside curly braces that performs the action.

Once your function has been declared, you can refer to it in code simply by utilizing the name that you’ve given it, such as:

greet

Since our functions perform actions, it is best to name them in a way that best describes the given action. PowerShell uses this approach when naming cmdlets, using action words such as Get, Write and Read.

Instructions

1.

Let’s create our first function!

Create a function named UserFeedback. Be sure to include opening and closing brackets.

2.

Add one or more write-host lines that provide positive feedback to the user.

3.

Add a line to call the function, just to ensure that when called, the correct output is displayed.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?