powershellwindows-usersget-wmiobject

Pull NT user ID from powershell


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?


Solution

  • 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.