Link to home
Start Free TrialLog in
Avatar of Janice Valencia
Janice Valencia

asked on

Active Directory Powershell Script

Powershell Script to import to a csv file users -> Lastname, Firstname, Windows logon ID, PhoneID from a specific OU (Customer Service)
Avatar of oBdA
oBdA

Try this:
Import-Module ActiveDirectory
$SearchBase = Get-ADOrganizationalUnit -Filter "Name -eq 'Customer Service'" | Select-Object -ExpandProperty DistinguishedName
Get-ADUser -Property telephoneNumber -Filter * -SearchBase $SearchBase |
	Select-Object Surname, GivenName, SamAccountName, TelephoneNumber |
	Export-Csv -NoTypeInformation -Path C:\Temp\CustomerService.csv

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Shaun Vermaak
Shaun Vermaak
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Janice Valencia

ASKER

Im getting this error: I am a newbie in AD powershell. Im sorry.

Get-ADUser : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'SearchBase'. \
Specified method is not supported
Then you have more than one "Customer Services" OU.
You could set $Searchbase directly to the DistinguishedName of the one you want, or do you want all of them?
EXPERT CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello, still gets object error. I am sure there is only one PSR OU and it exists.

domain name=abc.net
OU is like this: Windows7 > Windows7 Users > PSR

Please help. Thank you.
@ Shaun:

I tried Get-ADUser -filter * -Searchbase "OU=Customer Services,DC=Domain,DC=COM" -Properties Telephonenumber|Select sn, givenName,sAMAccountName, telephonenumber|Export-CSV -NoTypeInformation -Path C:\Temp\Export.csv

Changing the domain to ours dc=net and OU to PSR. But giving Directory object not found :(
Hello. I get it right this time :) Thank you all for the support.

I used: Shaun Vermaak's script:

Get-ADUser -filter * -Searchbase "OU=Customer Services,DC=Domain,DC=COM" -Properties Telephonenumber|Select sn, givenName,sAMAccountName, telephonenumber|Export-CSV -NoTypeInformation -Path C:\Temp\Export.csv

I did this to get th correct OU: 1. open Active Directory Users and Computers, enable Advanced Features in the menu, open the OU properties, go to Attribute Editor and open distinguishedName property and Select the OU distinguished name, copy/paste it to the above Get-ADUser command.

All good. Thank you all.
Thank you