I have a GPO that is linked to an OU containing computers. Loopback processing is activated an set to merge. Within the user part of the GPO i have a powershell script defined that basically checks if its running inside a remode desktop session (if ($env:SESSIONNAME -ne 'Console')) and then tries to set an environment variable based on a modification of $env:CLIENTNAME.

However the script seems not to work as at the time of execution there is no $env:CLIENTNAME. How can i fix this?

I have already tried to enable and disable "Run logon scripts synchronously". If i enable it - no luck, which seems logical. When set to "Not configured" the above mentioned happens. Thanks for your input.


When I invoke the script via "User Configuration => Policies => Administrative Templates => System => Logon => Run these programs at user logon" it seems to work. However not for every user and with a random latency.

Is there another way I can use powershell to query for %CLIENTNAME%? Like asking the remote desktop servies for logged in users... can you see where i am pointing at?


This link here helped me to solve my problem:


Now I am parsing the output of qwinsta and read the clientname from the registry which is earlier available then the var %CLIENTNAME%

Function Get-ClientName 
    $session = qwinsta $env:USERNAME
    $sessionId = $session[1].subString(39,9).Trim()

    return (Get-ItemProperty "HKCU:\Volatile Environment\${sessionId}\" -Name CLIENTNAME).CLIENTNAME

Then I am able to load my script from "User Configuration => Policies => Windows Settings => Scripts => Logon ..."

  • Thanks this function works like a charm! You're saving me a headache!!! – Charlie Vieillard Sep 24 '14 at 10:04

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.