Display name/value of both variable: and env: variables

This topic contains 3 replies, has 2 voices, and was last updated by  Scott Bass 4 years, 4 months ago.

  • Author
    Posts
  • #4403

    Scott Bass
    Participant

    Ok, I give up.  This should be simple, but I can't work it out.

    $foo="bar"
    
    $bar="blah"
    
    get-item variable:foo, variable:bar

    gives me:

    Name Value
    ---- -----
    foo bar
    bar blah

    So far, so good.

    But:

    get-item env:username, variable:foo, variable:bar

    gives me:

    Name Value
    ---- -----
    USERNAME sbass
    
    PSPath : Microsoft.PowerShell.Core\Variable::foo
    PSDrive : Variable
    PSProvider : Microsoft.PowerShell.Core\Variable
    PSIsContainer : False
    Name : foo
    Description :
    Value : bar
    Visibility : Public
    Module :
    ModuleName :
    Options : None
    Attributes : {}
    
    PSPath : Microsoft.PowerShell.Core\Variable::bar
    PSDrive : Variable
    PSProvider : Microsoft.PowerShell.Core\Variable
    PSIsContainer : False
    Name : bar
    Description :
    Value : blah
    Visibility : Public
    Module :
    ModuleName :
    Options : None
    Attributes : {}

    How can I get a nice tidy name/value pair tabular output for a combined env: and variable: listing?

    BTW, I know that env: is a dictionary and variable: is an object[].  I can't figure out how to cast one or the other into the right type to get the combined name/value output.

     

  • #4413

    Scott Bass
    Participant

    Hi,

    I've worked this out.  Google is my friend...

    get-item env:username, variable:foo, variable:bar | Format-Table Name,Value -AutoSize
    
    Name Value
    ---- -----
    USERNAME sbass
    foo bar
    bar blah

    or even:

    $x1=@{Label="Name"; Expression={$_.Name}; Width=30}
    get-item env:username, variable:foo, variable:bar | Format-Table $x1,Value
    
    Name Value
    ---- -----
    USERNAME sbass
    foo bar
    bar blah

     

    Apologies for not searching more before posting.

    (P.S.:  Output formatting is not preserved in the crayon formatting.  And I wish I could set courier (or other monospaced) font, and preview my post before submitting)

  • #4419

    Kamil Tatar
    Participant

    Hi,

    try this 🙂

    Get-Item env:username, variable:foo, variable:bar | Select-Object Name,Value | Format-Table -AutoSize
    
    Name     Value
    ----     -----
    USERNAME Kamil
    foo      bar
    bar      foo

     

    Regards, KmTatar

     

  • #4425

    Scott Bass
    Participant

    Thanks!!! 🙂

You must be logged in to reply to this topic.