I am a Linux guy.
Please don’t be confused that I’m often using the Linux equivalent aliases in powershell.

Environment variabes

Get Environment Variables

List all

> ls env:

Name                           Value
----                           -----
[...]

List specific

> ls env:USERPROFILE

Name                           Value
----                           -----
USERPROFILE                    C:\Users\<USERNAME>

Use wildcards

> l env:USER*

Name                           Value
----                           -----
USERNAME                       <USERNAME>
USERPROFILE                    C:\Users\<USERNAME>
USERDOMAIN_ROAMINGPROFILE      <DOMAIN>
USERDOMAIN                     <DOMAIN>
USERDNSDOMAIN                  <DOMAIN.de>

Set Environmant variable (persistent)

> [Environment]::SetEnvironmentVariable("THOR", "Yadda", "Machine")

where
THOR is the variable name
Yadda is the value of the variable
Machine is the scope in which to set the variable. Possible values ‘User’ / ‘Machine’

Open a new powershell to re-read the environment and check the variable is set

> ls env:THOR

Name                           Value
----                           -----
THOR                           Yadda

Remove variable from environment

rm env:THOR

Save Variables to a file

Save variables to a files for later usage

> hostname
Computer1

> $user = Get-ADUser user@domain -properties *

> $user.userprincipalname
user@domain

> $user | export-clixml ~/user.ps1xml

> cp -force ~/user.ps1xml \\files\transfer\

Open a new powershell and import the xml-exported variable
Or alternatively copy the file to another system and import it there, both works

> hostname
Computer2

> $user = import-clixml \\files\transfer\user.ps1xml

> $user.userprincipalname
user@domain

Leave a Reply