Removing characters from a string with -replace

Hi,

I’m trying to use this to change the output of the line below from Computername\Username, to simply Username
Get-wmiobject win32_computersystem | select username

When I use the code below, the output is @{username=\Username}

$User= get-wmiobject win32_computersystem -computer $ComputerName2.text | select username
$ID= $User-replace “Computername”, “”

Where am I going wrong on this one?

Thanks

That’s because $user isn’t a string. It’s an object having a Username property (you could see that if you piped it to Get-Member). If, instead, you did “select-object -expand username” then you’d get the behavior you expected. -Expand extracts the CONTENT of the property, in this case returning a simple string.