How can I sensibly get a Windows 7 user's username, but in the case that it's stored in the Active Directory domain (such as `BillyBird`), not using whatever case the user happened to type it when logging in (probably `billybird`, but could even be something like `bILlYbIRd`)?

This is in Perl, but since it's interacting with the operating system, solutions in other languages may be translatable.

I've tried these and they don't work; they all return the username in the case it was typed by the user when logging in\*:

* `getlogin`
* `Win32::LoginName`
* `$ENV{USERNAME}`
* `Win32::OLE->new('WScript.Network')->UserName`
* `Win32API::Net::UserGetInfo`

I have come up with a couple of bad ways of getting the right value. I'll post those as answers and hope that somebody else has a better one. Thanks.

\* At least they do some of the time. For some users it alternates each time they log in between giving the as-typed and the as-AD values, but in all cases the above methods all agree with each other and `%USERNAME%`.