Export Users Email Addresses from AD

Source Code

This script has not been checked by Spiceworks. Please understand the risks before using it.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

'Global variablesDimContainerDimOutPutFileDimFileSystem'Initialize global variablesSetFileSystem=WScript.CreateObject("Scripting.FileSystemObject")SetOutPutFile=FileSystem.CreateTextFile("smtpaddresses.csv",True)'Replace with valid DN of the container you want to enumerateSetobjRootDSE=GetObject("LDAP://RootDSE")SetContainer=GetObject("LDAP://"&objRootDSE.Get("defaultNamingContext"))'OR USE THIS Set Container=GetObject("LDAP://dc=OUHERE,dc=DOMAINHERE,dc=com")'Enumerate ContainerEnumerateUsersContainer'Clean upOutPutFile.CloseSetFileSystem=NothingSetContainer=Nothing'Say Finished when your doneWScript.Echo"Finished"WScript.Quit(0)'List all UsersSubEnumerateUsers(Cont)DimUser'Go through all Users and select themForEachUserInContSelectCaseLCase(User.Class)'If you find UsersCase"user"'Select all proxyAddressesDimAliasIfNotIsEmpty(User.proxyAddresses)Then'OutPutFile.Write User.DisplayNamestrAliases=""IfIsArray(User.proxyAddresses)=TrueThenForEachAliasinUser.proxyAddressesIfInStr(1,Alias,"smtp",1)>0ThenstrAddress=Replace(Alias,"smtp:","",1,1,1)arrAlias=Split(strAddress,"@")IfInStr(LCase(strAliases),LCase(arrAlias(1)))=0ThenIfstrAliases=""ThenstrAliases=""""&strAddress&""""ElsestrAliases=strAliases&","""&arrAlias(0)&""""EndIfEndIf'WScript.Echo User.DisplayName & vbTab & Replace(Alias,"smtp:","",1,1,1)EndIf'WScript.Echo AliasNextElsestrAliases=""""&Replace(LCase(User.proxyAddresses),"smtp:","")&""""EndIfOutPutFile.WriteLinestrAliasesEndIfCase"organizationalunit","container"EnumerateUsersUserEndSelectNextEndSub'*******************************************************