0

So in my Powershell script I have 2 variables getting multiplied by each other, but there is one problem, Powershell can't tell that I want to multiply the variables by each other. The script is below:

$answer = $a*$equation

The $a variable is 7. The $equation variable is 365*675/54 So what I want the script to do is 7*365*675/54 and set the answer to the $answer variable.

When I debug the script it says that the problem is in the * symbol. Can anyone help here?

2 Answers 2

3

I would use Invoke-Expression instead of running another PowerShell instance.

$answer = Invoke-Expression "$a*$equation"
Sign up to request clarification or add additional context in comments.

Comments

1

It sounds like the behavior you want is for Powershell to interpret the variable $equation as a mathematical; expression and not a value. In general this is not how variables work in powershell. They represent values, not expressions. Hence powershell sees this as a string and not a number.

This can be done though by spinning up another instance of powershell and asking it to interpret the string. It will then return the result which you can store

$resolved = powershell $equation
$answer = $a * $resolved

1 Comment

Thanks! I needed this for the rest of my script to function correctly and I couldn't find any answers on Google.

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.