3

Using PowerShell I can get all (current process) environment variables and format and sort them:

gci env: | %{"{0}={1}" -f $_.Name,$_.Value} | sort

which gives:

a=foo
b=bar
c=baz

But that gives all process variables, and I want only the user variables, so I do:

[Environment]::GetEnvironmentVariables("User") | %{"{0}={1}" -f $_.Name,$_.Value} | sort

But that outputs this only:

=

The [Environment]::GetEnvironmentVariables("User") works, the stuff after is the problem.

6

The issue is that [Environment]::GetEnvironmentVariables("User") returns a hash table, not a collection of objects. There are several ways to get what you want. Here is one:

([Environment]::GetEnvironmentVariables("User")).GetEnumerator() | %{'{0} = {1}' -f $_.Key, $_.Value}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.