Hi Spiceheads,

I’m a novice PS user and I have something I’d like to do via a script but I’m not sure how to get it done.

I have a list of ~150 names (formatted Last, First MI) in excel. Some have middle initials and others do not. I need to add all of these users to a DL.

I know I need a script to take names in the format I have and return a list of SAM account names, and then I need a script to take each SAM account name and add it to the DL I specify.

I’ve never really worked with powershell and csv’s before so any advice is very welcome!

Thanks!

5 Spice ups

We do not write sripts but helps you when you are stucked

check this topic

and this

1 Spice up

Thanks a lot!

I did not intend to come off like I wanted people to write things for me :slight_smile:

It looks like these threads will be very useful! Thanks again!

1 Spice up

So I’ve made some progress but I’m having issues getting the -filter syntax down. I’m using code copied from an example script that has worked for others but I get "Get-ADUser : the search filter cannot be recognized.

Here’s what I’m using. Any ideas?

$User = Get-ADUser -Filter "Givenname -like '*$FirstFilter*' -and Surname -like '*$SecondFilter*'" -Properties EmailAddress

Thanks!

Replying to myself here for sake of getting my solution out on the thread - my issue was with the formatting of my .csv. initially I had column A containing LastName, FirstName Initial.

I split that out so that column A was last names, column B was first names, etc. and it did the trick!

thanks for your assistance JitenSh! now comes the part where I get the info into the new DL.

1 Spice up

Thanks again for the assistance over here. I’ve created and populated my DL and things are good!

One last question on the topic.

Using the filter for first name and last name - how could I update the code to handle multiple hits? For instance:

If there are multiple users with the same name, such as John A. Smith, with SAM accounts of JASmith, JASmith1 and JASmith2 - is there a way to get the script to prompt for user selection in a situation like that? Can I get it to provide a list of names that match the filter along side other data from the account so I can make a selection?

for example:

1. JASmith  Chicago  CEO
2. JASmith1  Los Angeles  Analyst
3. JASmith2  Miami  Section Chief

Make a selection to continue: 

thanks!

on the link gungnirs script will tell you if account is missing, I am glad you are able to figure out, Best mentor and tutor is yourself

1 Spice up

Yep, I saw that and it works well but I’m also concerned with getting multiple hits, as opposed to a miss.

because it uses wild card in first name and last name suppose there is user name Jon snow and jon cena it may reflect both accounts

I prefer this way

#csv
user
Jon snow
ned stark

$list =  ipcsv C:\powershell\Test.csv
ForEach($user in $list){    
$dn = $user.user
Get-ADUser -Filter { displayName -like $dn } -Properties  EmailAddress | select Givenname,Surname,Samaccountname,EmailAddress |Export-Csv C:\temp\export1.csv -NoType -Append } 

1 Spice up

You can use Out-GridView to do this.

$ADUser = Get-ADUser -Filter "Givenname -like '*$FirstFilter*' -and Surname -like '*$SecondFilter*'"

$User = if( @($ADUser).Count -gt 1 ){
    $ADUser |
        Out-GridView -OutputMode Multiple
    }
else{
    $ADUser
    }

$User
1 Spice up

That out-gridview is pretty awesome!

How can I specify which columns I want from the aduser in the gridview?

Can I get it to put the location attribute from AD in a column? can I get it to not print the GUID and SID info?

thanks a million!

@gungnir

How can I specify which columns I want from the aduser in the gridview?

Can I get it to put the location attribute from AD in a column? can I get it to not print the GUID and SID info?

thanks a million!

Mostly by using a Select-Object :

$ADUser |
        Select-Object choose, property, names, here |
        Out-GridView -OutputMode Multiple

You may also need to request additional properties from the Get-ADUser cmdlet. Using the -Properties parameter.

1 Spice up