Hey Powershell Community,
I’d like to ask for first time a question or help for the Problem that I’m stuck at.
Example:
$var = "lol"
$var2 = "lol2"
$variantA = 'This is example with $var and $var2'
$variantB = "This is example with $var and $var2"
Basically I could get a string in my script where I have the names of variables written in String. Can anyone help me out or suggest how could I get values from variables that are in string written? In this example that means How could I get “lol” and “lol2” from variantA and variantB?
I would be so grateful for any answer or question.
$VariantB should already contain the expanded values of $var and $var2. Using double quotes will evaluate variables and “expand” their contents into the string. Single quotes are literal and will not expand the values.
If you just want to get the values of $var and $var2, simply run:
I didn’t get your question completely but to explain the difference for you - if you use single quotes expressions or veriables will not be executed/evaluated, if you use normal double quotes expressions and variables will be executed/evaluated.
Sorry, I re-read and thought that you might be looking for how to extract the values of the first two variables out of $VariantB. One way to do this is with regular expressions:
Hey,
thanks for such fast answer. I might haven’t explained it not so good.
What I am trying to show is:
Per example I have a Excel or .csv file that contains in one cell text or String: “This is the Computer with $ProcessorName and $GraphicCardName.”
I now want to get what it is in this $ProcessorName and $GraphicCardName String variable. Basically their values from StringName that is written in this line of String.
It is a Little bit hard to explain. I am basically trying to write a script for WDS Server for Software/Operating System Installation that will be deployed through Powershell script.
here’s a version using Regex, just in case you’re interested:
"This is the Computer with Pentium and Nvidia" -match "^This is the Computer with (?'Processor'\w+) and (?'GraphicCard'\w+)$"
$Matches.Processor
$Matches.GraphicCard