PowerShell makes it easy to pass values to a function in the form of parameters. However, it’s a good idea to validate parameters before executing any actions. Otherwise, unexpected values could cause a function to behave in a completely unpredictable way.
Before I get started, I want to point out that the techniques I’m about to demonstrate are only suitable for scenarios where you are passing a string value to a function and are certain that the string should contain one of several possible values. If you are working with other types of data or don’t know ahead of time which values for the string are acceptable, you must resort to using other validation techniques.
#1. Validation Check Using the -Contains Operator
So, with that said, here is a very simple PowerShell script that demonstrates the first validation technique:
Looking at the script, you’ll notice that I created a variable called $Colors and have set it to be equal to “Red”, “Green”, “Blue”, “Yellow”, “Gray”. These are the colors that the script will support. While PowerShell recognizes a broader range of color names, the script deliberately focuses on these five colors to keep things simple.

This is what my script does.
#2. Validation Check Using ValidateSet
Now that I have demonstrated how to perform parameter validation by using a -Contains operator, I will introduce a different technique that accomplishes more or less the same thing.
Here is the script:

There is a way to handle errors more gracefully, but requires you to be running PowerShell 7 or higher.

PowerShell 7 greatly improves error handling.
About the Author(s)

Brien Posey is a bestselling technology author, a speaker, and a 20X Microsoft MVP. In addition to his ongoing work in IT, Posey has spent the last several years training as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space.
Sometimes, you might need to pass an array as a parameter to a function in PowerShell. In this tutorial, I will explain everything about PowerShell array parameters. We will explore how to use arrays as parameters in PowerShell functions with a few examples.
In PowerShell, arrays are flexible—they can hold items of any type, including integers, strings, objects, or even other arrays.
To declare an array in PowerShell, you simply assign multiple values to a variable, separated by commas:
$myArray = 1, 2, 3, 4, 5This line of code creates an array $myArray containing five integer elements.
Passing an Array to a Function in PowerShell
When you want to pass an array to a function in PowerShell, you don’t need to do anything special—just pass the array variable as you would any other parameter. Here’s an example of a function that accepts an array parameter:
function Show-ArrayContents { param([array]$numbers) foreach ($number in $numbers) { Write-Host $number }
}
$myArray = 1, 2, 3, 4, 5
Show-ArrayContents -numbers $myArrayIn this example, the function Show-ArrayContents takes one parameter, $numbers, which is expected to be an array. The function then iterates over each item in the array and prints it to the host.
You can see the output in the screenshot below after I executed the PowerShell script using VS code.

Modifying PowerShell Arrays Within Functions
It’s important to understand that when you pass an array to a function, you are passing a reference to the original array. This means that if you modify the array within the function, those changes will be reflected outside the function as well. Here’s an example:
function Add-Element { param([array]$numbers) $numbers += 6
}
$myArray = 1, 2, 3, 4, 5
Add-Element -numbers $myArray
Write-Host $myArrayThis will output 1 2 3 4 5 6 because the function Add-Element adds an element to the original array.
Returning Arrays from a PowerShell Function
Functions in PowerShell can also return arrays. This is useful when you need to generate a list of items based on some logic and then work with that list outside the function. Here’s an example:
function Get-MultiplesOfTwo { param([int]$maxNumber) $multiples = @() for ($i = 2; $i -le $maxNumber; $i += 2) { $multiples += $i } return $multiples
}
$multiplesOfTwo = Get-MultiplesOfTwo -maxNumber 10
Write-Host $multiplesOfTwoThis function returns an array of multiples of two up to the specified maximum number.
Advanced Array Parameters
function Display-Messages { param([object[]]$messages) foreach ($message in $messages) { Write-Host $message }
}
Display-Messages -messages "Single message"
Display-Messages -messages "First message", "Second message"In this case, Display-Messages can be called with either a single object or an array of objects.
Conclusion
In this PowerShell tutorial, I have explained everything in detail about PowerShell array parameters with examples.
You may also like:



