Click here to Skip to main content
15,881,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
(get-wmiobject Win32_ComputerSystem –ComputerName "<user pc="" name="">").UserName
is returning the user name in the below format
<systemname>/<domainname>/<username>
The above User name format will get when i logged in with Microsoft credentials.

When i logged in with Local account to PC, the same command returning the below Username Format
<domainname>/<username>
Why it is returning
<systemname>/<domainname>/<username>
when i Logged in with microsoft account in Windows 8.1?

What I have tried:

i have tried splitting the commands to get proper username format.
Posted
Updated 16-Apr-19 4:58am
v4
Comments
Richard MacCutchan 16-Apr-19 8:59am    
I get a similar result, probably because UserName is fully qualified. Check the help to see if it can be reduced.

1 solution

That's normal. Powershell is returning whatever WMI is returning. WMI returns the fully qualified "domain\username". If you want just the user name, you're going to have to parse that string or split it on the "\" character and just return the string after the split.

You can do that like this:
(get-wmiobject Win32_ComputerSystem).UserName.Split('\')[1]
 
Share this answer
 
v2
Comments
Member 14047666 16-Apr-19 10:32am    
I worried why It is returning in the three format , appending PC name .
Because of Microsoft account logged-in?
Dave Kreskowiak 16-Apr-19 12:05pm    
No. The first item is the domain that holds the account. In a workstation environment, that would be the machine the user is logged into because that's where the SAM Account database is.

In a domain environment, the domain name would be the first part of the user name.
Member 14047666 17-Apr-19 0:58am    
Thanks Dave

(get-wmiobject Win32_ComputerSystem).UserName

the User Name will be the in the below format as follows.
<systemname>/<domainname>/<username>

System Name: lenovo-pc
DomainName: LENOVO-PC
UserName: TestAccount

The USERNAME and DOMAINNAME are same in the system registry.

Why PC Name is retrieving from the above WMI Query?
How it is associated with the Usernanme?
Dave Kreskowiak 17-Apr-19 7:50am    
I've never seen it returned like that. The line of code I posted above is very easily modified to return the 3rd item in the array returned by Split. Just change the 1 to a 2.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900