If you want to compare the return value of a function in a conditional expression, you should group the function call (i.e. put it in parentheses) or (as @FlorianGerhardt suggested) assign the return value of the function to a variable and use this variable in the conditional expression. Otherwise, the comparison operator and the other operand are passed as arguments to the function (where in your case they are silently discarded). Then your function returns a result that is neither "" , nor 0 , nor $null , so it evaluates to $true , causing both messages to display.
This should do what you want:
... if ( (Get-Platform) -eq 'x64' ) { echo "64 bit platform" } ...
By the way, you should avoid using separate if for conditions that are mutually exclusive. For platform check if..then..elseif
$platform = Get-Platform if ($platform -eq "x64") { ... } elseif ($platform -eq "x86") { ... }
or << 28>
Switch (Get-Platform) { "x86" { ... } "x64" { ... } }
will be more appropriate.
I would also avoid echo inside the function. Just return the value and perform any echo that may be required with the return value. Everything that is reflected inside the function will also be returned to the caller.
Last note: I personally would prefer not to rely on the existence of a specific folder or environment variable to determine the architecture of the operating system. Using WMI for this task makes me much more reliable:
function Get-Platform { return (gwmi Win32_OperatingSystem).OSArchitecture }
This function will return the string "32-Bit" or "64-Bit" , depending on the architecture of the operating system.
source share