简体   繁体   English

$env:username 返回旧用户名

[英]$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.我已经在 Windows 10 中更改了我的用户名,现在我需要自动将这个更改的用户名作为 Powershell 脚本获取,但是除了一些与 AD 相关的查询之外,我尝试的所有操作都会返回我的旧用户名。

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.因此,我无法相对获得正确的用户名以在 PS 脚本中使用。

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 .如果您尝试获取用于登录的用户名,它应该是([adsi]"WinNT://$env:userdomain/$env:username,user").Name而不是.fullname

EDIT: Environmental variables are not updated unless the application (Powershell in this case) is restarted .编辑: 除非应用程序(在这种情况下为 Powershell)重新启动,否则不会更新环境变量。 There are workarounds .解决方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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