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

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.

 

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)

Hi,

try this :slight_smile:

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

 

Thanks!!! :slight_smile: