As clarification for my comment $input
is a reserved automatic variable.
If you change it in your some-string-function
with another named variable you will have the expected behaviour.
Passing a string type to a function
-
03-12-2021 - |
Question
I have the following code:
cls
$input = 'Hello World'
Write-Host '$input.GetType() = ' + $input.GetType()
function some-string-function($input)
{
Write-Host 'In the function now'
Write-Host '$input.GetType() = ' + $input.GetType()
Write-Host '$input (value) = ' + $input
return $input.length
}
$result = some-string-function $input
"`$result = $result"
I am getting the following:
$input.GetType() = + System.String
In the function now
$input.GetType() = + System.Collections.ArrayList+ArrayListEnumeratorSimple
$input (value) = + System.Collections.ArrayList+ArrayListEnumeratorSimple
$result =
Can someone point out how do I actually pass a string to a function in PowerShell please?
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow