I have a problem trying to "fix" a PS script I got from a collegue to get me information from a customers AD. I'm trying to get a list of all distrobution mailboxes and members of these. But the script he gave me doesn't work, I've tried to change some but since I'm still learning PowerShell I'm finding myself stuck and really not understanding what his script is trying to do.

Groups = Get-ADGroup -Filter * -SearchBase OU=costumer,OU=companies,DC=domene,DC=oss"

$Results = foreach( $Group in $Groups ){

Get-ADGroupMember -Identity $Group | foreach {


        GroupName = $Group.Name

        Name = $_.Name



$Results| Export-Csv -Path c:\data\groups.csv -NoTypeInformation? -Encoding 

When I run this script I get an error from the very begining because of "Groups =" where Groups is not recognised.

So I have changed the start to:

Get-ADGroup -filter * -Searchbase "OU=Distribution,OU=Groups,OU=Costumer,OU=Companies,DC=domene,DC=oss"

This is the full path to the AD objects I want, and I have tested this commando and get the full list. But I don't get the information about members of the goupobjects. I've tried change on the bottom part of the script, but get an error returned on "-identity" all the time, it dosn't matter what I write after it.

Can someone help med with a better code to get out the information I need? And maybe explain what this script is trying to do?

Thank you very much for your help.


Thank you for the help! I now get the script to run and I get the .cvs file. But I'm wondering if I have something else wrong. It should return the groups in AD and members of the groups, but the result I get is not this. I've attached screenshot from when I open this in excel.

enter image description here

Is there something else missing in my script to get the information?


I found a way changing the script to make it get what I needed, and it worked. It might to a little more than only getting the information that I need, but I got the result that I needed. :) Thank you!

$GruppeMedlemmer = @()

$Groups = Get-ADGroup -Filter * -properties * -SearchBase "ou=felles,OU=Groups,OU=costumer,OU=Companies,DC=domene,DC=oss"

foreach ($g in $Groups) {
$members = $g | Get-ADGroupMember 
     foreach ($m in $members) {
       $Info = New-Object psObject 
       $Info | add-member -MemberType NoteProperty -Name "GroupName" -Value $g.Name
       $Info | add-member -MemberType NoteProperty -Name "Description" -Value $g.description
       $Info | add-member -MemberType NoteProperty -Name "Member of" -Value $g.MemberOf
       $Info | Add-Member -MemberType NoteProperty -Name "Member" -Value $m.name
       $GruppeMedlemmer+= $Info

$GruppeMedlemmer | Sort-Object GroupName | Export-CSV C:\temp\groupdist.csv -notype -Encoding unicode  
  • I figured out the begining of the script was missing $ infront of Groups. After adding this I'm able to run the script, but not get any file output. Is there something I'm missing? – Hege Jacobsen Jun 14 '18 at 11:41
  • Export-Csv : A parameter cannot be found that matches parameter name 'NoTypeInformation?'. At C:\scripts\groups2.ps1:18 char:70 + $Results | Export-Csv -Path c:\scripts\groups.csv -NoTypeInformation? <<<< -Encoding unicode + CategoryInfo : InvalidArgument: (:) [Export-Csv], ParameterBindingException + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.ExportCsvCommand – Hege Jacobsen Jun 14 '18 at 11:44
  • 1
    The trailing ? in -NoTypeInformation is the cause of the error, simply remove it. – LotPings Jun 14 '18 at 13:08

This should work:

$Groups = Get-ADGroup -Filter * -SearchBase "OU=dummy1,OU=dummy2,DC=dummy3,DC=dummy4"

$Results = foreach( $Group in $Groups ){

    Get-ADGroupMember -Identity $Group | foreach {

            GroupName = $Group.Name
            Name = $_.Name
$Results| Export-Csv -Path c:\data\groups.csv -NoTypeInformation -Encoding unicode
  • Missing $ in Line 1
  • Missing " in Line 1
  • Wrong ? in line 13
  • 'unicode' needs to be in the same line as Encoding

Make sure the directory C:\data exists

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.