Question about Get-Variable

Hey Powershell Community,
I’d like to ask for first time a question or help for the Problem that I’m stuck at.

$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.

Thank you ALL in advance.


$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:


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.

Thank you for taking time for this question.


Using your “This is the Computer with $ProcessorName and $GraphicCardName” example

$x = “This is the Computer with Pentium and Nvidia”
$y = ($x -split 'This is the Computer with ')[1] -split ’ and ’

PS> $y[0]
PS> $y[1]

Never did like regex :slight_smile:

Hey Richard,

Thanks for your post. I will try it out and will let you know.

Have a nice evening,


Hmmm … I guess I have a vage idea what you try to do. Try:


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+)$"

Hi Olaf, this is brilliant. Works like a charm. :slight_smile: Also a thank-you to all the others.

Tried it with the following unfancy codesnippet before (also works but is too long and unsexy…):


    Try {
        If (($LanguageFiles = Get-ChildItem ".\" | Where {$_.Name -like "Language_*.csv"} | Select-Object -ExpandProperty Name).count -gt 0)
                WRITE-DEBUG "Write-Feedback - `$LanguageFiles: $LanguageFiles"

                # Jede gefundene Datei in Katalog aufnehmen
                ForEach ($LanguageFile in $LanguageFiles) 
                    WRITE-DEBUG "Write-Feedback - `$LanguageFile: $LanguageFile"

# Note: Get-Delimiter is no build in CMDlet

                    # Delimiter ermitteln 
                    $Delimiter = Get-Delimiter ".\$LanguageFile" $Output $Log

                    WRITE-DEBUG "Write-Feedback - `$Delimiter: $Delimiter"

                    # Sprachdateien importieren
                    $LanguageCatalog = Import-CSV -Encoding Default -Delimiter $Delimiter ".\$LanguageFile" -ErrorAction Stop

                    WRITE-DEBUG "Write-Feedback - `$LanguageCatalog: $LanguageCatalog"

                    # Inhalt der Sprachdateien auflisten
                    $LanguageCatalogs += $LanguageCatalog

                    WRITE-DEBUG "Write-Feedback - `$LanguageCatalogs: $LanguageCatalogs"

        WRITE-DEBUG "Write-Feedback - `$PSUICulture: $PSUICulture"

        Try {
        # Programm:
        # Gibt die einzelnen Sätze aus für das gewählte Ereignis
        ForEach ($languageset in ($LanguageCatalogs.GetEnumerator() | Where {$_.MessageIDs -eq $Text}))
            WRITE-DEBUG "Write-Feedback - `$languageset: $languageset"

            # Gibt die einzelnen Wörter aus welche pro gewählte Sprache ($PSUICulture) anfallen
            $singlewords = $languageset.$PSUICulture.Trim().Split(" ")

            WRITE-DEBUG "Write-Feedback - `$singlewords: $singlewords"

# Note: Standardinfo-Error-Field-Empty is no build in CMDlet

            # Prüfen ob die Variable einen Wert enthält
            If (!$singlewords) {Standardinfo-Error-Field-Empty}

            # Array definieren
            $var = @()

            # Die einzelnen vorhandenen Variablen erfassen
            ForEach ($containedvar in ($singlewords | Where {$_ -like '$*'}))
                WRITE-DEBUG "Write-Feedback - `$containedvar: $containedvar"

                # Einzelne Variablen sammeln
                $var += $containedvar

                WRITE-DEBUG "Write-Feedback - `$var: $var"
                } # End-ForEach

            # Jedes einzelne Wort prüfen
            ForEach ($word in $singlewords)
                WRITE-DEBUG "Write-Feedback - `$word: $word"

                # Durchlaufendes Wort ersetzen
                # Nur Wörter wählen, die mit "$*" beginnen
                If ($word -like '$*')
                    # Jede einzelne Variable auf einen Treffer prüfen
                    ForEach ($subvar in $var) 
                        WRITE-DEBUG "Write-Feedback - `$subvar: $subvar"

                        # Pattern anwenden um angehängte Symbole zu entfernen
                        $wordtrim = $word -replace '(\$[a-zA-Z0-9]+).*','$1'
                        $subvartrim = $subvar -replace '(\$[a-zA-Z0-9]+).*','$1'
                        $subvartrimvar = $subvartrim.trim('$')

                        WRITE-DEBUG "Write-Feedback - `$wordtrim: $wordtrim"
                        WRITE-DEBUG "Write-Feedback - `$subvartrim: $subvartrim"
                        WRITE-DEBUG "Write-Feedback - `$subvartrimvar: $subvartrimvar"

                        # Variable erschaffen / abrufen
                        $replace = $subvartrim

                        WRITE-DEBUG "Write-Feedback - `$replace: $replace"
                        $with = (Get-Variable $subvartrimvar -Valueonly -ErrorAction Stop) 
                        WRITE-DEBUG "Write-Feedback - `$with: $with"

                        # Prüfen ob der Name der Variable mit der generierten 100%tig übereinstimmt
                        If ($replace -eq $wordtrim)
                            WRITE-DEBUG "Write-Feedback - `$replace: $replace"
                            WRITE-DEBUG "Write-Feedback - `$wordtrim: $wordtrim"

                            # Text-Variable mit gewünschtem Wort (variablen Inhalt) ersetzen
                            $newword = $word.Replace("$replace","$with")

                            WRITE-DEBUG "Write-Feedback - `$newword: $newword"
                            # Nur die ersetzten Variablen übernehmen, welche nicht den gleichen Namen aufweisen z.B.: nicht $testen -eq $testen
                            If ($newword -ne $word)
                                # Neues Wort dem Satz hinzufügen
                                $newset = $newset + $newword + " "

                                WRITE-DEBUG "Write-Feedback - `$newset: $newset"
                                } # End-If
                            } # End-If
                        } # End-ForEach
                    } # End-If
                    # Wörter zu einem Satz zusammenfügen die kein "$" beinhalten
                    $newset = $newset + $word + " "

                    WRITE-DEBUG "Write-Feedback - `$newset: $newset"
                    } # End-Else
                } # End-ForEach
            } # End-ForEach  

Rob & Bernard

You’re welcome. Glad it was helpful. :wink: