Pull NT user ID from powershell

筅森魡賤 提交于 2021-02-02 09:54:27

问题


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?


回答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.



来源:https://stackoverflow.com/questions/25693818/pull-nt-user-id-from-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!