string和powershell从数组中提取用户名

时间:2017-07-02 18:14:38

标签: string powershell

我有这个代码,当我执行此PowerShell代码时:

Get-CimInstance –ClassName Win32_ComputerSystem | Select-Object UserName 

我会得到:

UserName               
--------               
DESKTOP-JBJN9QA\joli

我怎样才能只提取名称joli 谢谢

1 个答案:

答案 0 :(得分:2)

-Expandproperty UserName对我不起作用 - 所以如果你不想要

Get-CimInstance –ClassName Win32_ComputerSystem | Select-Object PrimaryOwnerName

尝试

(Get-CimInstance –ClassName Win32_ComputerSystem).UserName.split('\')[1]