pwsh
🧩 Syntax:
# Define the current user's SAMAccountName
$samAccountName = $env:USERNAME
# Bind to the root of the domain
$rootDSE = [ADSI]"LDAP://RootDSE"
# Get the default naming context (domain components)
$defaultNamingContext = $rootDSE.defaultNamingContext
# Bind to the directory searcher for the domain
$directorySearcher = New-Object System.DirectoryServices.DirectorySearcher
$directorySearcher.SearchRoot = [ADSI]"LDAP://$defaultNamingContext"
$directorySearcher.Filter = "(&(objectClass=user)(sAMAccountName=$samAccountName))"
# Perform the search to get the user's directory entry
$userEntry = $directorySearcher.FindOne()
if ($userEntry -ne $null) {
# Get the LDAP path for the user
$ldapPath = $userEntry.Path
$user = [ADSI]$ldapPath
# Modify the extraAttribute2 (replace with the desired value)
$user.Put("extensionAttribute2", "NewValue")
# Commit the changes to AD
$user.SetInfo()
Write-Host "extensionAttribute2 has been updated for $samAccountName."
} else {
Write-Host "User $samAccountName not found in AD."
}