0
0
PowerShellscripting~15 mins

Return values in PowerShell - Mini Project: Build & Apply

Choose your learning style9 modes available
Return values
📖 Scenario: You are writing a simple PowerShell script to calculate the area of rectangles. You want to create a function that takes the width and height as inputs and returns the area.
🎯 Goal: Build a PowerShell function that returns the area of a rectangle given its width and height, then display the result.
📋 What You'll Learn
Create a function named Get-Area that takes two parameters: width and height.
Inside the function, calculate the area by multiplying width and height.
Return the calculated area from the function.
Call the function with specific values and print the returned area.
💡 Why This Matters
🌍 Real World
Functions that return values are used in scripts to perform calculations or data processing and send results back for further use.
💼 Career
Understanding how to write functions with return values is essential for automating tasks and creating reusable code in IT and DevOps roles.
Progress0 / 4 steps
1
Create the function skeleton
Create a function called Get-Area with two parameters: width and height. Do not add any code inside the function yet.
PowerShell
Need a hint?

Use the function keyword and param() block to define parameters.

2
Calculate the area inside the function
Inside the Get-Area function, create a variable called $area that multiplies $width and $height.
PowerShell
Need a hint?

Use $area = $width * $height to calculate the area.

3
Return the area from the function
Add a return statement inside the Get-Area function to return the value of $area.
PowerShell
Need a hint?

Use return $area to send the result back.

4
Call the function and display the result
Call the Get-Area function with width 5 and height 10, store the result in a variable called $result, then print $result.
PowerShell
Need a hint?

Use $result = Get-Area -width 5 -height 10 and Write-Output $result to show the area.