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@
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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
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@
PowerShell Script:
$string = 'lastname title initial@department'
$string.Substring(0, $string.IndexOf('@'))
Write-Output $string
--------
input: lastname title initial@department
output: lastname title initial
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
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