Using Powershell to retrieve mail attributes
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.
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.
A slight cleaner version of this would be:
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:
Reference Link: http://technet.microsoft.com/en-us/library/ee617241.aspx
Dan
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
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
ASKER
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"
$OutputFile = "MyOutputFile.csv"
ASKER
Nevermind,,,,, I'm and idiot worked great!!!!!! Thank you
ASKER
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?
$OutputFile = "MyOutputFile.csv" , where do I stick this variable at if I use it,, or is it being used?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
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.
ASKER
Expert knows his stuff. Hope all people with questions can find him.
ASKER
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.cs v"
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\Docume nts\Script _Commands\ attibutesb ysearchbas e.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], ADIdentityNotFoundExceptio n
+ FullyQualifiedErrorId : Directory object not found,Microsoft.ActiveDire ctory.Mana gement.Com mands.GetA DUser
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?
import-module ActiveDirectory
$SearchBase = "OU=*Laptops,OU=Laptop Users,DC=oxarc1,DC=int"
$SearchScopeControl = "OneLevel"
$OutputFile = "C:\temp\EmailAddresses.cs
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\Docume
+ Get-ADUser <<<< -filter * -SearchBase $SearchBase -SearchScope $SearchScopeControl -Properties * | select Name, SamAccountName, UserPrincipalName, EmailAddress| Export-Csv $OutputFile -noTypeInformation -UseCulture
+ CategoryInfo : ObjectNotFound: (:) [Get-ADUser], ADIdentityNotFoundExceptio
+ FullyQualifiedErrorId : Directory object not found,Microsoft.ActiveDire
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?
ASKER
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.
Open in new window
Just update the SearchBase value and run.
Dan