user9686961
user9686961

Reputation:

Incredibly slow running Get-ADUser Array - Can I speed it up?

I've been working with the following code for awhile, and it works but it takes hours to run. If I run separate Get- commands I get the results within minutes but as soon as I add the array in it scales up to hours.

I might be biting off more than I can chew with this as I'm still fairly new to PS as I don't need to use it often.

  Import-Module Activedirectory

              $Data=@(

              Get-ADUser  -filter * -Properties * |  
              Select-Object @{Label = "First Name";Expression = {$_.GivenName}},  
              @{Name = "Last Name";Expression = {$_.Surname}},
              @{name=  "OU";expression={$_.DistinguishedName.split(',')[1].split('=')[1]}},
              @{Name = "Email";Expression = {$_.Mail}},
              @{Name = "Account Status";Expression = {if (($_.Enabled -eq 'TRUE')  ) {'Enabled'} Else {'Disabled'}}},
              @{Name = "Department";Expression = {$_.Department}}

                     )

              $Data | Export-Csv -Path c:\adusers.csv -NoTypeInformation      

Upvotes: 3

Views: 2603

Answers (2)

user9686961
user9686961

Reputation:

Runs in seconds!

Import-Module Activedirectory

$Data=@(                  
Get-ADUser  -Filter {Enabled -eq $true} -SearchBase “ou=User Accounts,ou=UserAccounts,dc=hiscox,dc=com” -Properties EmailAddress, extensionattribute2 |  
Select-Object @{Label = "First Name";Expression = {$_.GivenName}}, @{Name = "Last Name";Expression = {$_.Surname}}, @{Name = "Email";Expression = {$_.EmailAddress}}, @{Name = "Business Area";Expression = {$_.extensionattribute2}}
        )

$Data | Export-Csv -Path c:\adusers.csv -NoTypeInformation      

Upvotes: 1

Clayton Lewis
Clayton Lewis

Reputation: 394

Selecting only what you need will make it go much faster. I am going to start using this method for the environment I work in. I learned a few things from this question

Import-Module Activedirectory

$Data=@()

$Data = Get-ADUser -Filter * -Properties "Mail","Department" | 
    Select-Object @{Label = "First Name";Expression = {$_.GivenName}},  
    @{Name = "Last Name";Expression = {$_.Surname}},
    @{Name=  "OU";expression={$_.DistinguishedName.split(',')[1].split('=')[1]}},
    @{Name = "Email";Expression = {$_.Mail}},
    @{Name = "Account Status";Expression = {if (($_.Enabled -eq 'TRUE')  ) {'Enabled'} Else {'Disabled'}}},
    @{Name = "Department";Expression = {$_.Department}}

$Data | Export-Csv -Path c:\logs\adusers.csv -NoTypeInformation

Upvotes: 1

Related Questions