简体   繁体   中英

$env:username returns old username

I've changed my username in Windows 10, and now I need to automate getting this changed username as a Powershell script But everything I try returns my old username apart from some AD-related query.

Example:

    $env:UserName

    whoami

    $(Get-WMIObject -class Win32_ComputerSystem | select username).username

Return my old username while my user was actually renamed.

Because of this I can't relatively get correct username to use in PS scripts.

Only thing that was able to extract it was this:

    ([adsi]"WinNT://$env:userdomain/$env:username,user").fullname

But on other machine it just returns empty result

If you're trying to get the username used for signon, it should be ([adsi]"WinNT://$env:userdomain/$env:username,user").Name not .fullname .

EDIT: Environmental variables are not updated unless the application (Powershell in this case) is restarted . There are workarounds .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM