locked
Evaluate hashtable value inside double quotes? RRS feed

  • Question

  • I have a hashtable:

    $h = @{target='A'}

    I can get $h["target"] which will be 'A'. How do I evaluate $h["target"] inside double quotes? I have tried "This is $($h["target"])" but there are too many double quotes and I think PowerShell gets confused. Ideas?

    Thank you.

    Kevin


    Kevin Burton

    Wednesday, October 16, 2013 4:22 PM

Answers

  • or, since Kevin seems to prefer the "[]" notation, this also works:

    
    PS M:\> "Value of $($h['target'])"
    Value of A
    PS M:\>


    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    • Proposed as answer by Yan Li_ Thursday, October 17, 2013 2:51 AM
    • Marked as answer by KevinBurton Thursday, October 17, 2013 2:53 AM
    Wednesday, October 16, 2013 5:11 PM

All replies

  • "This is "  $h["target"]

    "This is "$h.target


    Thanks Azam When you see answers please Mark as Answer if Helpful..vote as helpful.

    Wednesday, October 16, 2013 4:29 PM
  • I have a hashtable:

    $h = @{target='A'}

    I can get $h["target"] which will be 'A'. How do I evaluate $h["target"] inside double quotes? I have tried "This is $($h["target"])" but there are too many double quotes and I think PowerShell gets confused. Ideas?

    Thank you.

    Kevin


    Kevin Burton

    Better way to access hash value :

    PS C:\Temp> $h.target
    A

    PS C:\Temp> "Value of $($h.target) "
    Value of A


    MCITP - Exchange 2010 | MCITP - Windows Server 2008 R2

    • Proposed as answer by Yan Li_ Thursday, October 17, 2013 2:51 AM
    Wednesday, October 16, 2013 4:33 PM
  • or, since Kevin seems to prefer the "[]" notation, this also works:

    
    PS M:\> "Value of $($h['target'])"
    Value of A
    PS M:\>


    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    • Proposed as answer by Yan Li_ Thursday, October 17, 2013 2:51 AM
    • Marked as answer by KevinBurton Thursday, October 17, 2013 2:53 AM
    Wednesday, October 16, 2013 5:11 PM