Removing characters from a string with -replace

Welcome Forums General PowerShell Q&A Removing characters from a string with -replace

This topic contains 1 reply, has 2 voices, and was last updated by

5 years, 4 months ago.

  • Author
  • #11755

    Points: 0
    Rank: Member


    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?


  • #11756

    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    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.

The topic ‘Removing characters from a string with -replace’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort