Forum Discussion
Baron164
Jun 24, 2022Brass Contributor
Need help importing user data from csv
I need to fill in the Manager Attribute for employee accounts using a CSV export from HR. I have the added bonus of the fact that SamAccountNames do not follow a consistent naming convention. So I ne...
- Jun 28, 2022
Hey, Harm.
I'd strongly recommend using the -Filter parameter in the Get-AD* calls, as you're current pulling every single user in Active Directory client-side before filtering anything - twice.
In smaller environments, that won't be an issue, but it'll run into scalability issues rather quickly.
Using your original example, something like this would be more efficient in medium to large directories:
$csv = import-csv c:\scripts\accounts.csv foreach ($line in $csv) { $user = Get-ADUser -Filter "employeeID -eq '$($line.EmployeeID)'"; # Here, we're now leveraging server-side filtering. $manager = Get-ADUser -Filter "employeeID -eq '$($line.SupervisorID)'"; # And again here. if ($user -is [Microsoft.ActiveDirectory.Management.ADUser] -and $manager -is [Microsoft.ActiveDirectory.Management.ADUser]) { # We only want to be in here where we got precisely one user and one manager, otherwise we have no way to arbitrate who gets updated with what. Set-ADUser $user -Manager $manager } }
Cheers,
Lain
Jun 27, 2022
Baron164 I've just tested this in my test DC and this should work:
$csv = import-csv c:\scripts\accounts.csv
foreach ($line in $csv) {
$user = Get-ADUser -Filter * -Properties EmployeeID | Where-Object EmployeeID -eq $line.EmployeeID
$manager = Get-ADUser -Filter * -Properties EmployeeID | Where-Object EmployeeID -eq $line.SupervisorID
Set-ADUser $user -Manager $manager
}
together with this CSV file
"LAST_NAME","FIRST_NAME","EmployeeID","SupervisorID"
"User1","Test","1","3"
"User2","Test","2","3"
Which results into this (Test User2 as example having Test user 3 as manager now )
LainRobertson
Jun 28, 2022Silver Contributor
Hey, Harm.
I'd strongly recommend using the -Filter parameter in the Get-AD* calls, as you're current pulling every single user in Active Directory client-side before filtering anything - twice.
In smaller environments, that won't be an issue, but it'll run into scalability issues rather quickly.
Using your original example, something like this would be more efficient in medium to large directories:
$csv = import-csv c:\scripts\accounts.csv
foreach ($line in $csv) {
$user = Get-ADUser -Filter "employeeID -eq '$($line.EmployeeID)'"; # Here, we're now leveraging server-side filtering.
$manager = Get-ADUser -Filter "employeeID -eq '$($line.SupervisorID)'"; # And again here.
if ($user -is [Microsoft.ActiveDirectory.Management.ADUser] -and $manager -is [Microsoft.ActiveDirectory.Management.ADUser])
{
# We only want to be in here where we got precisely one user and one manager, otherwise we have no way to arbitrate who gets updated with what.
Set-ADUser $user -Manager $manager
}
}
Cheers,
Lain
- Jun 28, 2022I did think about that, but thought that I couldn't use the Filter command on that 🙂 But this is better for larger environments with more objects of course