Removing characters from a string with -replace

This topic contains 1 reply, has 2 voices, and was last updated by  Don Jones 3 years, 8 months ago.

  • Author
    Posts
  • #11755

    Barry Thomson
    Participant

    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

  • #11756

    Don Jones
    Keymaster

    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.

You must be logged in to reply to this topic.