1

I don't know much about Powershell but I try to learn. Can you help me to understand and solve what I want to do:

$string1=""

$complicatedString1 = "This is a complicated $string1"
$complicatedString2 = "$complicatedString1 too"

$string1 = "Test"

$complicatedString1 -> Should state now: This is a complicated Test
$complicatedString2 -> Should state now: This is a complicated Test too.

$string1 = "question"

$complicatedString1 -> Should state now: This is a complicated question
$complicatedString2 -> Should state now: This is a complicated question too.

and so on.

The idea sounds simple: I want to define strings that serve as templates, but with variable content. In this case I want to manipulate $string1 and update $complicatedstring 1 and 2 so that any change of $string1 gets reflected in those strings.

$string1 would change frequently and at the moment I have no approach how get the changed values into those strings. Basically the're just placeholders to wait for being changed.

3
  • 1
    Your description is still a little vague. The content of a variable is defined in the moment you assign it. If $String1 is still empty and you define another variable including $String1 it will stay empty. You would need to re-assign the new variable AFTER you assigned something to $String1 to reflect this change in the new variable. You might show or explain a little bit more of your use case. Commented Jan 3, 2020 at 8:48
  • Does this answer your question? How to expand variable in powershell? Commented Jan 3, 2020 at 9:08
  • Put your complicatedString1and complicatedString2 in single quotes and than use: $ExecutionContext.InvokeCommand.ExpandString($complicatedString1) Commented Jan 3, 2020 at 9:24

3 Answers 3

1

I'm not quite sure, but I think your question is about using template strings you can alter using new strings-to-insert.

The most elegant way to do that I think is by making use of the PowerShell -f format operator like this:

# the template strings
$complicatedString1 = "This is a complicated {0}"
$complicatedString2 = "{0} too"

# use the template strings to return new strings
$string1 = $complicatedString1 -f "Test"                # --> "This is a complicated Test"
$string2 = $complicatedString2 -f $string1              # --> "This is a complicated Test too"

$string1 = $complicatedString1 -f "question"            # --> "This is a complicated question"
$string2 = $complicatedString2 -f $string1              # --> "This is a complicated question too"

You can also use the Format method of the string object itself if you like:

$string1 = [string]::Format($complicatedString1, "Test")
$string2 = [string]::Format($complicatedString2, $string1)

which will give you the exact same results

Sign up to request clarification or add additional context in comments.

1 Comment

Great answer which helped me to solve my problem satisfactory.
0

For this you need to put you complicatedString1 and complicatedString2 in single quotes (otherwise they will be directly expanded), than you might use the $ExecutionContext.InvokeCommand.ExpandString method:

$complicatedString1 = 'This is a complicated $string1'
$complicatedString2 = '$complicatedString1 too'

$string1 = 'Test'

$ExecutionContext.InvokeCommand.ExpandString($complicatedString1)
This is a complicated Test
$ExecutionContext.InvokeCommand.ExpandString($ExecutionContext.InvokeCommand.ExpandString($complicatedString2))
This is a complicated Test too

$string1 = "question"

$ExecutionContext.InvokeCommand.ExpandString($complicatedString1)
This is a complicated question
$ExecutionContext.InvokeCommand.ExpandString($ExecutionContext.InvokeCommand.ExpandString($complicatedString2))
This is a complicated question too

Note that you will need to use the ExpandString method twice on the $complicatedString2 to get to the bottom of the $string1 expansion.

To get a better overview, you might consider to create a recursive function with a -Depth parameter:

Function Expand([String]$String, [Int]$Depth = 1) {
    $Expand = $ExecutionContext.InvokeCommand.ExpandString($String)
    If ($Depth-- -ge 0) {Expand $Expand $Depth} Else {$Expand}
}

$string1 = 'Test'

Expand $complicatedString1
This is a complicated Test
Expand $complicatedString2 -Depth 2
This is a complicated Test too

1 Comment

Also a great answer with very useful information. The other answer was slightly more useful in my case though as it shows a way to have exact control over what to substitute.
0

You can also do it via a loop

do {
    $string1 = Read-Host "Please enter a word"
    $complicatedString1 = "This is a complicated $string1"; $complicatedString1
    $complicatedString2 = "$complicatedString1 too`n"; $complicatedString2
} while ($string1 -ne "exit program")

Output

Please enter a word: Test
This is a complicated Test
This is a complicated Test too

Please enter a word: Question
This is a complicated Question
This is a complicated Question too

Please enter a word: Exit Program
This is a complicated Exit Program
This is a complicated Exit Program too
#Program Exits#

1 Comment

Code only answers are not encouraged here, can you explain the context how this answer will help..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.