How to use PowerShell reference variable

In this tutorial, we will discuss what is a reference variable in PowerShell? And how to create and use the PowerShell reference variable.

Also, how to use a reference variable in a function and how to pass variables to a function by reference in PowerShell?

[toc]

Finally, we will discuss how to fix error Cannot process argument transformation on parameter ‘parameter name’. Reference type is expected in argument error which comes while working with reference variables.

Now we will see how to create and use it in PowerShell.

PowerShell reference variable

By default whenever we will create a variable in PowerShell, it will be created and accessible at the local scope.

But by using a PowerShell reference variable, we can pass values to a function.

How to create and use a reference variable in PowerShell?

In PowerShell, we mostly use reference variables while passing parameters to functions.

We can pass a variable to a function either by value or by reference.

When we pass a variable to a function by value, then we actually pass a copy of the data. So the value will be always unchanged outside the scope of the function.

Below is a PowerShell method which is taking a parameter as value type.

Function Calculate($num)
{
    $num = 50
}

$var = 100
Calculate -num $var
Write-Host $var

If you check the output, it will display the value as 100 which was outside of the function scope.

PowerShell reference variable
PowerShell reference variable

But if you pass the parameter as reference type the value of the variable will be changed.

To pass the value as a reference type, we pass by using the [ref] keyword.

Function Calculate([ref]$number)
{
    $number.Value = 50
}
$var = 100
Calculate -number ([ref]$var)
Write-Host $var

Now, if you will check the value of the variable then you can see the value has been changed. It is no more taking the local scope value.

create and use PowerShell reference variable
create and use PowerShell reference variable

PowerShell Pass by reference multiple parameters

In PowerShell, we can also pass by reference multiple parameters.

We can pass two reference type variables to a PoweShell function.

Function GetFullName([ref]$fname, [ref]$lname)
{
    $fname.Value = "Bhawana"
    $lname.Value = "Rathore"
}
$firstname = "Bijay"
$lastname = "Sahoo"
GetFullName -fname ([ref]$firstname) -lname ([ref]$lastname)
Write-Host $firstname" "$lastname

Once you run the script, you can see the PowerShell variable value will be changed.

PowerShell Pass by reference multiple parameters
PowerShell Pass by reference multiple parameters

PowerShell reference type is expected in argument

While working with this PowerShell reference type I got the below error:

Calculate : Cannot process argument transformation on parameter ‘number’. Reference type is expected in argument.
At line:6 char:19
Calculate -number [ref]$var
CategoryInfo : InvalidData: (:) [Calculate], ParameterBindingArgumen
tTransformationException
FullyQualifiedErrorId : ParameterArgumentTransformationError,Calculate

And here I was writing the code like below:

Function Calculate([ref]$number)
{
    $number.Value = 50
}
$var = 100
Calculate -number [ref]$var
Write-Host $var

The error was because of the way I was passing reference parameter to the function.

Here we need to add the reference type variable name within the braces (). And When using a [ref] parameter within a function refer to $variable.value. You should call like below:

Calculate -number ([ref]$var)

You may like following PowerShell tutorials:

Conclusion

In this tutorial we discussed What is a PowerShell reference variable and how to create and use a reference variable.

And also checked, how to pass multiple parameters by reference in PowerShell and how to fix the error, PowerShell reference type is expected in argument.

>