1

I want to have a function in which the result is stored in a variable where the function defines the variable name - essentially this:

function testfunction ($varname,$text){
    $readhost = read-host -prompt "$text"
    new-variable -name $varname -value $readhost
}

though when entering:

testfunction outputvar sampletext

get-variable -name outputvar

I just get an error that the variable "outputvar" doesnt exist. what am I missing here? Tried a number of other stuff but nothing seemed to work - what i want in the end is a variable that is named "outputvar" that contains the prompt input, just to clarify.

1
  • This is due to scoping. Variables created with a function are scoped (only accessible) to that function. You can use $global: to create a global variable, but it would be better to just return a value to a variable. I'll write an answer with an example. Commented Mar 29, 2018 at 9:32

2 Answers 2

5

Your issue is due to scoping. The variable created via New-Variable is (by default) scoped so that it is only accessible within your function. You can override the scope via the -Scope parameter:

function testfunction ($varname,$text){
    $readhost = read-host -prompt "$text"
    new-variable -name $varname -value $readhost -scope Script
}

This changes the scope to Script and so the variable is now accessible outside of your function. Defining non-standard scopes for your variables isn't very good practice however. You should instead just be doing this:

function testfunction ($text){
    read-host -prompt "$text"
}

$outputvar = testfunction sampletext
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, i tried to keep the variable in the function so i dont have to repeat it for every use for the function - just out of curiousity, why exactly is it not a good practice? edit: i used $readhost = read-host in the first place because i was using a do - while loop though, but i guess that could work with a pipeline instead
Its just because scopes exist for a reason, to keep variables where they are needed and avoid confusion/errors that could occur if the same variable name was used elsewhere. If you wanted to loop around read-host you could still do that and then just output its value at the end: while (-not $readhost) { $readhost = read-host }; $readhost
forgot to add, im looping it while input is $null - im not quite sure how i should implement your example code, can you explain a little what this does?
2

Use the -OutVariable common parameter. To have your function support common parameters, add the CmdletBinding attribute decorator to the param block:

function Test-Function {
  [CmdletBinding()]
  param($text)

  return Read-Host -Prompt $text
}

Test-Function -OutVariable sampletext |Out-Null
$sampletext

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.