Exporting script output to csv

Mirceyhun Musayev
Mirceyhun Musayev used Ask the Experts™
Hi, i have script for get all groups that a user is member of. Works correctly, but  i cant export it to csv file. output shows PS terminal just. any ideas what i exactly must  add to script?


Import-Module ActiveDirectory
Get-ADUser -SearchBase "OU=Users,DC=domain,DC=local" -Filter * | foreach-object {
write-host "User:" $_.Name -foreground green
    Get-ADPrincipalGroupMembership $_.SamAccountName | foreach-object {
        write-host "Member Of:" $_.name
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Try this:
Import-Module ActiveDirectory
Get-ADUser -SearchBase "OU=Users,DC=domain,DC=local" -Filter * | ForEach-Object {
	$user = $_
	Write-Host "User:" $_.Name -ForegroundColor Green
	Get-ADPrincipalGroupMembership -Identity $_.SamAccountName | Select-Object -Property `
		@{n='User'; e={$user.SamAccountName}},
		@{n='Member of'; e={$_.Name}},
} | Export-Csv -NoTypeInformation -Path C:\Temp\MemberList.csv

Open in new window

Mirceyhun MusayevSystem Administrator


Thank you, it works

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial