Powershell Function Return Not What I Expected

October 24th, 2010 by Mitchell Lintzen

I had a Powershell Function that was supposed to return an Integer data type but occasionally would return an array or “system.object” type. I was checking the value with a $var.GetType().Fullname just before the “return” in the function and then after in the main body of the script and I could see that the value and the type of the variable was being changed. I finally found this article that explained output in Powershell:

http://keithhill.spaces.live.com/Blog/cns!5A8D2641E0963A97!811.entry

I had a “$string -match” inside of the function. That returns a True/False and then creates and array of the regular expression matches ($matches[x]). So every time I would see something like “True 58” as the return instead of just “58”. Then I found this article that helped to pipe the Boolean return of –match to $null:

http://www.tigraine.at/2010/09/22/powershell-functions-are-a-different-kind-of-beast/

I’ve had to use the redirect to null before but it’s been a long time. It all clicked once I understood how Powershell returns data from functions.

Leave a Reply

Bitnami