Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I parse data from a Get-WMIObject query into a string?

Tags:

powershell

wmi

I have the following line of code...

get-wmiobject -class win32_computersystem | select-object username

It returns (redacted with placeholders)...

@{username=DOMAIN\jsmith}

What needs to be done to remove the padding and give me a "plain" readout of DOMAIN\jsmith?

For bonus points, how do I parse that value into just jsmith?

like image 514
Bigbio2002 Avatar asked Feb 17 '23 02:02

Bigbio2002


1 Answers

You need to expand the property to get the value of username instead of a custom object with the property username. Try

get-wmiobject -class win32_computersystem | select-object -expand username

To get the username only, try:

(get-wmiobject -class win32_computersystem | select-object -expand username).Split("\")[2]

You may need to use [1] instead of [2] at the end depending on your OS. In Windows 8, you need 2, while in Windows 7(and older I think), you need 1.

like image 112
Frode F. Avatar answered Mar 16 '23 13:03

Frode F.