I am displaying all users in a group with this:
Import-Module ActiveDirectory
$Groups = Get-ADGroup -Filter {name -like "UC_*"} | Select -ExpandProperty Name # UC_* is my group
foreach ($Group in $Groups){
Write-Output "Group"
Write-Output "-----"
$Group
Write-Output ""
Get-ADGroupMember -Identity $Group | select -Property Name, samaccountname
Write-Output ""
}
I want to output this to a CSV but when I do, it looks like each line is getting overwritten by the next line in the loop so there is no data in the CSV. This is not working properly:
# Get users in a group or groups
Import-Module ActiveDirectory
Function Get-Users {
$Groups = Get-ADGroup -Filter {name -like "UC_*"} | Select -ExpandProperty Name # UC_* is my group
foreach ($Group in $Groups){
Write-Output "Group"
Write-Output "-----"
$Group
Write-Output ""
Get-ADGroupMember -Identity $Group | select -Property Name, samaccountname
Write-Output ""
}
}
Get-Users | export-csv "c:\temp\myfile.csv"
How can I output all content to a CSV properly?