Set-ADUser JDoe -surname "$LastName"
Set-ADUser JDoe -surname (Read-Host "Enter last name")
$User = Read-Host "Type the UserName"
$AD_User_Change = Read-Host "1 = LASTNAME. 2 = FIRST NAME. 3 = DEPARTMENT. 4 = TITLE. 5 = TELEPHONE NUMBER. 6 = EMPLOYEE ID. 7 = EMPLOYEE NUMBER"
switch ($AD_User_Change)
{
1 {$InputLastName = Read-Host "Type the new Last Name"
$NewLastName = Read-Host "Are you sure, you want to replace the following lastname $AD_USER.sn by $InputLastName (Y/N)"
if ($NewLastName -eq "y"){
Set-ADUser $User -surname $InputLastName -ErrorAction SilentlyContinue -ErrorVariable Err1
If($Err1.Count -eq 0){
Write-Host "Set new LastName $InputLastName for $user"}
Else{
Write-Host "Failed to set new LastName $InputLastName for $user"}
}
}
2 {"Your code for FIRST NAME here"}
3 {"Your code for DEPARTMENT here"}
4 {"Your code for TITLE here"}
5 {"Your code for TELEPHONE NUMBER here"}
6 {"Your code for EMPLOYEE ID here"}
7 {"Your code for EMPLOYEE NUMBER here"}
default {"Your code if none of above selected"}
}
Set-ADUser -surname $InputLastName -DisplayName $($InputLastName+","+$AD_USER.givenName)
Set-ADUser $AD_User.sAMAccountName -surname $InputLastName -DisplayName $($InputLastName+","+$AD_USER.givenName)
Note that you'll also want to update the displayname if you change the firstname, so just use the same principle as used here, but the code bit would be like -DisplayName $($InputFirstName+","+$AD_Set-ADUser -surname $InputLastName -DisplayName $($InputLastName+","+$AD_USER.givenName)
andSet-ADUser $AD_User.sAMAccountName -surname $InputLastName -DisplayName $($InputLastName+","+$AD_USER.givenName)
Get-ADUser $AD_User.sAMAccountName | Set-ADUser -surname $InputLastName -ErrorAction SilentlyContinue -ErrorVariable Err1
the identity for the Set-ADUser command is being supplied from the pipeline from Get-ADUser.
Also, here's another link with similar info for background: http://poshoholic.com/2009/06/23/powershell-quick-tip-setting-ad-object-attributes-with-scriptblock-parameters/