Question about Get-Variable

This topic contains 10 replies, has 5 voices, and was last updated by  Olaf Soyk 1 year, 8 months ago.

  • Author
  • #57884


    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.


  • #57887

    Matt McNabb

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

  • #57890

    Olaf Soyk

    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.

  • #57893

    Matt McNabb

    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:

  • #57901


    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.


  • #57925

    Richard Siddaway

    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 ­čÖé

  • #57928


    Hey Richard,

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

    Have a nice evening,


  • #57929

    Olaf Soyk

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

  • #57931

    Matt McNabb

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


    Hi Olaf, this is brilliant. Works like a charm. ­čÖé 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

    • #58006

      Olaf Soyk

      You're welcome. Glad it was helpful. ­čśë

You must be logged in to reply to this topic.