Hi,
It's possible only through Powershell because the value of environment variable is not static. Below a example of script that you can adapt it to create new environment variable:
$DN_list = Get-ADComputer -Filter * | select -ExpandProperty DistinguishedName
foreach($DN in $DN_list){
$DN = $DN.Replace(",OU=","$")
$DN = $DN.Split("$")
$DN =$DN[1]
$DN = $DN.Split("-")
$DN = $DN[0]
invoke-Command -ComputerName $computerName -ScriptBlock{$env:OU = $DN}
}
Please don't forget to mark helpful reply as answer