Forum Discussion
charlie4872
Jun 18, 2020Brass Contributor
Using Remote $env user variable with invoke-command not working
In short, the script below is taking the $env:APPDATA info from my local computer instead of the $env:APPDATA info for the user currently logged on at remote computer, specified in the $computer vari...
gastone
Jun 27, 2020Brass Contributor
The variable APPDATA is a user enviroment variable and is present only in the user session...
APPDATA is builded runtime, in the user session!
So if you invoke locally (or remotely) your code or the following line
Get-CimInstance -ClassName win32_environment |Where{$_.name -match "appdata"}
you get nothing, and is the correct final result
The "no default" user variables are present in the registry in Computer\HKEY_CURRENT_USER\Environment (appdata is not present)
APPDATA is builded using %USERPROFILE% (session variable) and the string \AppData\Roaming
So you are approching the problem in the wrong way, that is the reason you getting crazy...
How to resolve the problem?
Get the remote users profile (or a single remote user) build the appdata and remove the teams blob storage
#returning "remote" builded appdata variables
(Get-CimInstance -ClassName win32_userprofile -ComputerName $computer |select localpath)| %{ "{0}\appdata\roaming" -f $_.localpath}
Bye Gas