Read all user from windows AD

$ExportPath = 'C:\Users\administrator\ad-export'

$groupNames = (Get-ADGroup -Filter * -SearchBase "OU=Groups,OU=Users,DC=local,DC=exmaple")

foreach ($group in $groupNames) {
$fileName = (Get-ADGroup $group | Select-Object SamAccountName | Out-String -Stream)
$fileName2 = (
$fileName.
replace('@{Name=','').
replace('}','').
replace('\','').
Replace('SamAccountName','').
Replace('----------','').
replace('\\r\\n','').
replace('\r\n','').
replace('----','').
replace(' ','')
)
$path = $ExportPath + "\" + $fileName2 + ".csv"
Get-ADGroup $group | Get-ADGroupMember -Recursive | Get-ADUser -Properties * | Select SamAccountName, Surname, GivenName, Title, Mail | Export-Csv -NoType -Encoding UTF8 -Delimiter ";" $path
}