Dear Mr. @Andreas Baumgarten iam so sorry for disturbing you again but iam lost :(( I created a tested lab with few members. and iam still not able to change it. Can you help me please? I did this after few hours and this work for me but next step is impossible for me.
Split path
$Path = Split-Path -Parent "C:\scripts*.*"
Create variable for the date stamp in log file
$LogDate = Get-Date -f yyyyMMddhhmm
Define CSV and log file location variables
They have to be on the same location as the script
$Csvfile = $Path + "\AllADUsers_$logDate.csv"
Import Active Directory module
Import-Module ActiveDirectory
Set distinguishedName as searchbase, you can use one OU or multiple OUs
Or use the root domain like DC=exoip,DC=local
$DNs = @(
"OU=Users,OU=LAB,DC=lab,DC=local"
)
Create empty array
$AllADUsers = @()
Loop through every DN
foreach ($DN in $DNs) {
$Users = Get-ADUser -SearchBase $DN -Filter * -Properties *
# Add users to array
$AllADUsers += $Users
}
Create list
$AllADUsers | Sort-Object Name | Select-Object `
@{Label = "First name"; Expression = { $.GivenName } },
@{Label = "Last name"; Expression = { $.Surname } },
@{Label = "Display name"; Expression = { $.DisplayName } },
@{Label = "User logon name"; Expression = { $.SamAccountName } },
@{Label = "User principal name"; Expression = { $.UserPrincipalName } },
@{Label = "Street"; Expression = { $.StreetAddress } },
@{Label = "City"; Expression = { $.City } },
@{Label = "State/province"; Expression = { $.State } },
@{Label = "Zip/Postal Code"; Expression = { $.PostalCode } },
@{Label = "Country/region"; Expression = { $.Country } },
@{Label = "Job Title"; Expression = { $.Title } },
@{Label = "Department"; Expression = { $.Department } },
@{Label = "Company"; Expression = { $.Company } },
@{Label = "Manager"; Expression = { % { (Get-AdUser $.Manager -Properties DisplayName).DisplayName } } },
@{Label = "Description"; Expression = { $.Description } },
@{Label = "Office"; Expression = { $.Office } },
@{Label = "Telephone number"; Expression = { $.telephoneNumber } },
@{Label = "E-mail"; Expression = { $.Mail } },
@{Label = "Mobile"; Expression = { $.mobile } },
@{Label = "Notes"; Expression = { $.info } },
@{Label = "Account status"; Expression = { if (($.Enabled -eq 'TRUE') ) { 'Enabled' } Else { 'Disabled' } } },
@{Label = "Last logon date"; Expression = { $.lastlogondate } }|
Export report to CSV file
Export-Csv -Encoding UTF8 -Path $Csvfile -NoTypeInformation #-Delimiter ";"
So I have CSV but now I don't know what to do. it should be easy but iam lost. Thank you for your help again. Jiri