Remove characters in display name after special character

Ingrid 61 Reputation points
2022-07-01T20:06:09.433+00:00

I am pretty new to powershell and hope I can get some ideas to my problem.

The display names of the users in our OU are set up to look like this: 'lastname title initial@department@location'
The display name is created manually, so the lastname, title and initial are not retrieved from any other field.

When users go to a different department and location we move them to a transfer OU, so the OU in that location can retrieve the account.
When we move the users we remove everything after the first @, so only the 'lastname title inital @' remain in the display name.

I have been looking for a script to remove everything after the first @ without changing anything in front of the @ but did not found anything.

Is this even possible? If yes, please let me know how to do this?

Thank you

Windows for business Windows Client for IT Pros Directory services Active Directory
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2022-07-01T21:35:01.23+00:00

    You can also use a regex to accomplish the task:

    $string = 'lastname title initial@department'  
      
    $x = $string -replace "^(.+?@).*$", '$1'  
    

    The result (in $x) is:

    lastname title initial@

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Dillon Silzer 57,826 Reputation points Volunteer Moderator
    2022-07-01T20:12:24.677+00:00

    PowerShell Script:

    $string = 'lastname title initial@department'
    $string.Substring(0, $string.IndexOf('@'))

    Write-Output $string

    --------

    input: lastname title initial@department
    output: lastname title initial


  2. Alcala, Jesus 6 Reputation points
    2022-07-04T15:11:13.39+00:00

    Use Split in PowerShell

    $string = 'lastname title initial@department@location'  
     $loginname = $string.Split('@')[0]  
      
     Write-Host `The user login name is: $loginname`  
    

    In the variable $LoginNane you will have only lastname title initial


  3. Alcala, Jesus 6 Reputation points
    2022-07-04T19:56:17.707+00:00

    With Set-ADUser first you must to know which options you will update

    The command would be something like this

    Set-ADUser -Identity $loginname -Replace @{title="manager"}  
    

    Here can see all the fields available

    set-aduser

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.