Your script looks mostly correct, but there are a few things to check to ensure it runs smoothly:
- Check the CSV Format: Ensure that the CSV file
C:\Tmp\TestAccount.csv
is correctly formatted and that the delimiter used is indeed a semicolon (;
). If the file uses commas, you should change-Delimiter ';'
to-Delimiter ','
. - Property Names: Make sure that the property names in the CSV (like
telephoneNumber
andsAMAccountName
) match exactly with the headers in your CSV file. PowerShell is case-sensitive when accessing properties. - Error Handling: Consider adding error handling to catch any issues that may arise when calling
Set-ADUser
. You can useTry-Catch
blocks for this.
Here’s a slightly modified version of your script with error handling:
$Users = Import-Csv C:\Tmp\TestAccount.csv -Delimiter ';'
foreach ($user in $Users) {
$Hashtable = @{
OfficePhone = $user.telephoneNumber
}
try {
Set-ADUser -Identity $user.sAMAccountName @Hashtable
} catch {
Write-Host "Error updating user $($user.sAMAccountName): $_"
}
}
This will help you identify any issues that occur during the execution of Set-ADUser
.
References: