Monthly Archives: October 2012

Editing extensionattribute15 on a user with Powershell

We used to run Exchange (2003) internally, but no longer do. However we wanted to add a value to a user, where the user, and the value, were included in a CSV file. So if you want to add/modify a users extensionattribute with Powershell, this is how you can do it!


#Variables - edit as required
################################################################
$server = "gregdom.int"
$csvfile = "emea to groupid.csv"
################################################################

$userstrings = Import-Csv $csvfile

Foreach ($userstring in $userstrings) {
	$Username = $userstring.Username
	$Value = $userstring.ValuetoAdd

	$DN = (Get-adUser $username -Server $server).DistinguishedName
 	$User = [ADSI]"LDAP://$DN"
 	$User.Put("extensionAttribute15", $value)
 	$User.SetInfo()	
				
	}

Leave a comment

Filed under Uncategorized

Rename AD users with Powershell

We need to change the name of our users in Active Directory. Not just the displayname, but also the distinguishedname. The following script will show you how to do that:

#Rebrand-Users.ps1

#Variables - edit as required
################################################################
$Sitecode = "HQ"
$container = "OU=ImportedUsers,DC=GREGDOM,DC=INT"
$server = "gregdom.int"
$company = "MyCompany"
################################################################

#Read users into variable.
$users = get-aduser -filter * -SearchBase $container
 
#Process users.
Foreach ($user in $users) {
    $newDN = $user.surname + ", " + $user.GivenName + " ($sitecode)"
	#Change display name, and company name
	set-aduser $user -Displayname $newDN -company $company -server $server -ErrorAction SilentlyContinue
	#Change distinguished name
		Try {
    		Rename-ADObject -identity $user -Newname $newDN -server $server 
		}
		Catch {
			Write-Host "$user may already exist."
		}
    }

Note that it is basically a two step process, once to change the display name, and then again to change the distinguished name, which cannot be done with set-aduser.

Leave a comment

Filed under Microsoft, Powershell