Forum Discussion

rmerritt's avatar
rmerritt
Tin 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

6 Replies

  • server_gov's avatar
    server_gov
    Copper Contributor

    I am a fan of regular expressions and in this instance you could use the following

     

    $reg_replace = [string]”{0}” -f “^.+=|}$”
    $vcentrename = $vcentrename -replace $reg_replace

     

    how does this work 

    match group 1

    • ^ - anchor start of string
    • . - match any single non space character 
    • + - repeat matching any single non space character as required
    • = - match literal character =

    logic or

    • | - match everything before and after

    match group 2

    • } - match literal character }
    • $ - anchor end of string 

    replace

    • regular expression replace the match with no value

    outcome $vcentrename

    • original - @{Name=WEBHOST001-OI3w}
    • replace - WEBHOST001-OI3w
  • You're seeing this output because $vcentername is not an object with a Name property — it's a string that only looks like @{Name=...}. That's why $vcentername.Name returns nothing.

    If the value is literally:

    $vcentername = '@{Name=WEBHOST001-OI3w}';

     

    then $vcentername is just a plain string:

    @{Name=WEBHOST001-OI3w}

     

    You can extract the actual name like this:

    ($vcentername.Split("=")[1]).TrimEnd("}")

     

    which returns:

    WEBHOST001-OI3w

     

    To help you get the cleanest solution, could you show the command that produces $vcentername?

    If the source returns a real object, you won’t need to parse anything.

  • 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
    Tin 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