0

i try to create a text in a variable that every time I call it takes the current value in the variable "$ name_person".

In the first round you should give:

Hi Victor
Hi Juan
Hi Pedro
Hi Luis

I'm testing with [ref] but it doesn't take it anyway.

Is there any way to do it without functions or without "replace tag"?

Thank you

$name_person = 'victor'

$message = "hi " + $name_person + ':'


$names = @('Juan', 'Pedro', 'Luis')


foreach ($name in $names){

    $name_person = $name
    $message 

}

Response:

hi victor:
hi victor:
hi victor:

2 Answers 2

1

You can assign ScriptBlock value to $message and invoke it in your loop:

$message =  { "hi $name_person"}
$names = @('Juan', 'Pedro', 'Luis')
foreach ($name in $names){
  $name_person = $name
  & $message 
}
Sign up to request clarification or add additional context in comments.

Comments

0

It is not necesary to create ScriptBlock, just use variable substitution

$names = @('Juan', 'Pedro', 'Luis')
foreach ($name in $names) {"hi $name"}

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.