0
get-wmiobject -class win32_computersystem -computername c73118 | format-table username

Will output something similar to:

username
--------
GHS_NTDOMAIN\amacor

Is it possible to only output the amacor part only?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Aaron
  • 3,135
  • 20
  • 51
  • 78
  • there's always the %USERNAME% environment variable, which is just the username component, without domain. – Marc B Sep 05 '14 at 20:54
  • 1
    @MarcB I'm fairly adept with powershell and batch scripting, but I have no idea how to use your comment to get a user name from a remote computer. I think it is safe to assume he is looking at a remote computer since he specifies `-computername` in his `get-wmiobject` call. – TheMadTechnician Sep 05 '14 at 21:20

1 Answers1

1

first, you don't really want FT for this I don't think. Use Select -Expand instead. So doing that we get back the string GHS_NTDOMAIN\amacor. Once you have that, you can do .Split("\") to split it into an array of strings, and [-1] to specify the last string in the array. So it would look like:

(get-wmiobject -class win32_computersystem -computername c73118 | Select -ExpandProperty username).Split("\")[-1]

That will result in:

amacor

Or if you wanted to be a bit more verbose about it, you can do:

$Data = get-wmiobject -class win32_computersystem -computername c73118
$DomainUser = $Data.Username
$UserName = $DomainUser.Split("\")[-1]

Then $UserName = "amacor"

Edit: Updated per Andy Arismendi's excellent suggestion.

TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56