3

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      
  • 5
    Don't use `-Properties *`. Instead, specify only the specific properties you want (actually, you need only specify `Mail` and `Department`, as the others are standard properties that get included by default). – Bill_Stewart Jul 06 '18 at 13:34
  • @Bill_Stewart, nice call. That makes a tremendous difference. I tested on my small test domain (only 450 users) and it was about 16 times faster when pulling just the specific properties. – boxdog Jul 06 '18 at 13:47
  • Excellent @Bill_Stewart I'm working with about 45k users so it's been a bit of a annoyance! Thank you! –  Jul 06 '18 at 13:49
  • 1
    Incidentally, `if (($_.Enabled -eq 'TRUE') ) {'Enabled'} Else {'Disabled'}` is not doing what you think it is. It's working by coincidence of how the string`'TRUE'` gets cast to a boolean type. It ought to be `if ($_.Enabled) {'Enabled'} Else {'Disabled'}` or `if ($_.Enabled -eq $true)...` – TessellatingHeckler Jul 06 '18 at 14:02
  • You can use `@{Name = "Account Status"; Expression = {if ( $_.Enabled ) { "Enabled" } else { "Disabled" }} }` – Bill_Stewart Jul 06 '18 at 14:50
  • You may want to read up on [my answer here](https://stackoverflow.com/questions/51218670/how-to-effectively-use-the-filter-parameter-on-active-directory-cmdlets) for why you should avoid `-Properties *` and `-Filter *` where possible, though your case above does look to be a valid case for processing all `ADUser` objects. – codewario Jul 12 '18 at 15:37

2 Answers2

1

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
Clayton Lewis
  • 394
  • 2
  • 16
  • `Enabled` is a boolean type. You should just put `if ($_.Enabled) {`. Also, `Department";E={$_.Department}` is wasteful, just use `-Property Department` as a parameter to the select. – Maximilian Burszley Jul 06 '18 at 15:41
1

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