Jump to content

Recommended Posts

Posted
$csvFile = "n:\csv.csv"
$csv = Import-Csv $csvFile

foreach ($line in $csv)
{
   $name = $line.name
   $email = $line.email

   Get-ADUser -filter 'mail -eq $email'
}

  • Thanks 1
Posted

You don't.

 

I'm assuming you will be developing the code further to "do" something else, so I made it easier to have the name and email in variables ready for using.

Posted

This will call get-aduser each time.

It would be better to get all of your users. Limiting the results where you can, then filter the results against the spreadsheet.

Something similar to this:

$adUsers = get-aduser -SearchBase "OU=Staff, OU=Users, DC=domain, DC=com" -Filter {enabled -eq $true}
$csv = import-csv file.csv
$adUsers.where({$_.UserPrincipalName -like $csv.UPN})

Posted

I knew someone would say that :)

 

I make scripts up as I go along so I'd have been doing

 

$allusers = get-aduser
for (line in csv)
{
 for user in $allusers
 {
 }
}

which I wouldn't have been proud of.

 

It depends if he has more $csvlines or more $users.

Posted

why does this return nothing for $ADUsersfromCSV?

 

$ADUsers = get-aduser -SearchBase "OU=Students,DC=school,DC=com" -Filter {enabled -eq $true}

$CSVUsers = Import-Csv 'D:\User_Export.csv'

$ADUsersfromCSV = $ADUsers | where {$_.UserPrincipalName -like $CSVUsers.Email}

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...