Forum Discussion

rmerritt's avatar
rmerritt
Copper Contributor
Jan 15, 2026

extract a string from this @{Name=WEBHOST001-OI3w}

Hi All I just want to be able to get the value

WEBHOST001-OI3w

from 

@{Name=WEBHOST001-OI3w}

 

the variable storing it is called 

$vcentername

4 Replies

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    Hi rmerritt​,

     

    Looking at your second post, it's just a string variable. It's purely coincidental that the value of the string happens to look similar to a valid PowerShell [hashtable] declaration.

     

    The reason it's not a valid PowerShell [hashtable] declaration is the assigned value of "WEBHOST001-OI3w" is not surrounded by quotes (single or double will suffice for this example). If it were surrounded by quotes, you could call Invoke-Expression to treat the string as a parsable command as shown below in the first two examples (the first example shows the conversion to a [hashtable] while the second goes one step further to list the value of the "Name" key):

     

     

    The third example from above (noting your value underlined in red which has no quotes) is how you can extract the string using string manipulation. But this is specific to the format of the value held in your $vcentername variable (which per your second post shows it to be a [string]) and won't work for different formats.

     

    If you apply the third example to your variable, you get this as the code line you want to use to extract just the host name:

     

    $vcentername.Split("=")[1].Replace("}", "");

     

    Cheers,

    Lain

  • rmerritt's avatar
    rmerritt
    Copper Contributor

    hmmm that doesnt seem to be an option here is what I see

    PS G:\merrr1\scripts> $vcentername
    WEBHOST001-OI3w

    PS G:\merrr1\scripts> $vcentername.name

    PS G:\merrr1\scripts> $vcentername.GetType()

    IsPublic IsSerial Name                                     BaseType                                                                                                       
    -------- -------- ----                                     --------                                                                                                       
    True     True     String                                   System.Object                                                                                                  

     

Resources