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?
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.

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 m

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 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:
- Create folder if not exists using PowerShell
- Create an encrypted password file in PowerShell and use in SharePoint online
- Create file if not exists with name as today’s date using PowerShell
- How to check file size using PowerShell Script
- The term is not recognized as the name of a cmdlet function PowerShell
- How to create and use PowerShell global variable
- PowerShell find files modified in last 24 hours and PowerShell get last modified time of files in folder
- PowerShell find files modified in last N days
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.
I am Bijay a Microsoft MVP (8 times –Â My MVP Profile) in SharePoint and have more than 15 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com