Solved

Using Powershell to retrieve mail attributes

Posted on 2014-11-12
10
1,069 Views
Last Modified: 2014-11-14
I need some help.  I am trying to use Get-ADuser cmdlet to retrieve the mail attributes of each user.  What I would really like is to just export into a .csv the name and the mail attribute.  I tried this using ISE.

import-module ActiveDirectory
Get-ADuser -filter * -properties mail| Export-Csv C:\temp\Computer.csv

That way I could modify the .csv but it did not import the mail.  Really could use some help. Thanks much in advance.
0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
10 Comments
 
LVL 28

Expert Comment

by:Dan McFadden
ID: 40439518
I would use the following:

Get-ADUser -filter * -SearchBase "OU=Your,OU=Structure,OU=GoesHere,DC=YouDomainName,DC=Extension" -Properties * | select Name, SamAccountName, UserPrincipalName, EmailAddress | Export-Csv EmailAddresses.csv -noTypeInformation -UseCulture

Open in new window


Just update the SearchBase value and run.

Dan
0
 
LVL 28

Expert Comment

by:Dan McFadden
ID: 40440079
A slight cleaner version of this would be:

import-module ActiveDirectory
$SearchBase = "OU=Your,OU=Structure,OU=GoesHere,DC=YouDomainName,DC=Extension"
$SearchScopeControl = "OneLevel"
$OutputFile = "MyOutputFile.csv"

Get-ADUser -filter * -SearchBase $SearchBase -SearchScope $SearchScopeControl -Properties * | select Name, SamAccountName, UserPrincipalName, EmailAddress | Export-Csv EmailAddresses.csv -noTypeInformation -UseCulture

Open in new window


This does the same as the first post, but it is easier to edit when you want to reuse it.  And the "SearchScope" option let's you control how deep in the OU structure the script runs.

Options for $SearchScopeControl are:
1. Base
2. OneLevel
3. Subtree

From Microsoft:
A Base query searches only the current path or object. A OneLevel query searches the immediate children of that path or object. A Subtree query searches the current path or object and all children of that path or object

Reference Link:  http://technet.microsoft.com/en-us/library/ee617241.aspx

Dan
0
 
LVL 4
ID: 40441039
Put in the information from Powershell ISE.  But where is the csv?  Does it have something to do with this?  Do I = it to something?

$OutputFile = "MyOutputFile.csv"
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 4
ID: 40441046
Nevermind,,,,, I'm and idiot worked great!!!!!!  Thank you
0
 
LVL 4
ID: 40441285
Going to close this out.  Just curious.  But what does this mean?

$OutputFile = "MyOutputFile.csv" ,  where do I stick this variable at if I use it,, or is it being used?
0
 
LVL 28

Accepted Solution

by:
Dan McFadden earned 500 total points
ID: 40442065
$OutputFile = "MyOutputFile.csv" ,  where do I stick this variable at if I use it,, or is it being used?

That is the path to the result set.  This is where the output of the powershell command is saved.  And, it is not being used but I've updated the code... again.  It was a long day for me yesterday.  Sorry...

import-module ActiveDirectory
$SearchBase = "OU=Your,OU=Structure,OU=GoesHere,DC=YouDomainName,DC=Extension"
$SearchScopeControl = "OneLevel"
$OutputFile = "EmailAddresses.csv"

Get-ADUser -filter * -SearchBase $SearchBase -SearchScope $SearchScopeControl -Properties * | select Name, SamAccountName, UserPrincipalName, EmailAddress | Export-Csv $OutputFile -noTypeInformation -UseCulture

Open in new window


You will notice that the $Outputfile is now in the last pipe section of the command.  You can update the 3 variables at the start of the code to adjust the AD OU, the depth at which you want to search and where you want the results saved.

The $Outputfile can be just a filename or a complete path to the file.  Both of these are valid:

$OutputFile = "C:\reports\email\EmailAddresses.csv"
$OutputFile = "EmailAddresses.csv"

When using just a filename, the csv will be saved in the same location as where the script is located.

Dan
0
 
LVL 4
ID: 40442936
You are awesome...  I know those long days too.  But go into the weekend that you really helped me out with this!!!!!!   Much appreciated,,, and thank you so much.
0
 
LVL 4

Author Closing Comment

by:get-ADuser -F ($_.Name -eq "Todd")
ID: 40442943
Expert knows his stuff.  Hope all people with questions can find him.
0
 
LVL 4
ID: 40443328
HELP,,,,  need one more thing.  I have OU's that equal *Laptops, and under that I have Laptop Users OU.  I have this

import-module ActiveDirectory
$SearchBase = "OU=*Laptops,OU=Laptop Users,DC=oxarc1,DC=int"
$SearchScopeControl = "OneLevel"
$OutputFile = "C:\temp\EmailAddresses.csv"
Get-ADUser -filter * -SearchBase $SearchBase -SearchScope $SearchScopeControl -Properties * | select Name, SamAccountName, UserPrincipalName, EmailAddress| Export-Csv $OutputFile -noTypeInformation -UseCulture

But I get this error.  

Get-ADUser : Directory object not found
At C:\Users\tmb.OXARC1\Documents\Script_Commands\attibutesbysearchbase.ps1:5 char:11
+ Get-ADUser <<<<  -filter * -SearchBase $SearchBase -SearchScope $SearchScopeControl -Properties * | select Name, SamAccountName, UserPrincipalName, EmailAddress| Export-Csv $OutputFile -noTypeInformation -UseCulture
    + CategoryInfo          : ObjectNotFound: (:) [Get-ADUser], ADIdentityNotFoundException
    + FullyQualifiedErrorId : Directory object not found,Microsoft.ActiveDirectory.Management.Commands.GetADUser
 
I hope this pasted right on this comment box.  But it works with *Laptops OU but not the OU underneath *Laptops which is Laptop Users.   I tried all three scopeControls but it didn't work.  

Can you help me one last time?
0
 
LVL 4
ID: 40443353
Nevermind.  I got it.  I see better how it works.  I have to only put the base OU,  then change the value of $SearchScopeControl.   Getting better at it.  Only question then is,  If I have 4 sub OU's in *Laptops  how to I get only 1 Sub OU that I need.  e.g.  OU=*Laptops, but I only want Laptop Users is Spokane or Yakima.  OU structure would be "OU=*Laptops,OU=Yakima Users". <-----  Only want Yakima not Spokane which is also in the *Laptops OU?     Hope I made sense.
0

Featured Post

Free NetCrunch network monitor licenses!

Only on Experts-Exchange: Sign-up for a free-trial and we'll send you your permanent license!

Here is what you get: 30 Nodes | Unlimited Sensors | No Time Restrictions | Absolutely FREE!

Act now. This offer ends July 14, 2017.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Group policies can be applied selectively to specific devices with the help of groups. Utilising this, it is possible to phase-in group policies, over a period of time, by randomly adding non-members user or computers at a set interval, to a group f…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question