Create CSV & HTML showing AD Group, Group Description, ManagedBy (Friendly Name)

Copper Contributor

I need some help doing a search of AD groups with the Description starting with "Admin Group*" and get the Managedby friendly name. Some groups are managed by groups and other are by user. I've tried putting together something using other script examples but, I'm able to get the data but can't figure out how to export the results to CSV & HTML. Here is what I have so far:

 

 

 

 

Get-ADGroup -filter { Description -like "Admin Group*"} -Properties CN, Description, ManagedBy | Sort-Object "CN" | ForEach-Object {
$GroupName = $_.CN;
$Description = $_.Description;
$Manager = $_.Managedby;
if (!$Manager) {
    $Manager = 'N/A';
}

Get-ADUser -Filter * -SearchBase $Manager -Properties * {
    $GivenName = $_.GivenName;
    $Surname = $_.Surname;
    $managerName = $GivenName, $Surname;
}
if (!$ManagerName) {
    (Get-ADGroup -Filter * -SearchBase $Manager -Properties Name) | ForEach-Object {
        $managerName = $_.Name;  
    }
}

Write-Output $Groupname, $Description, $ManagerName}

 

 

It writes the output like this though:

GroupName

Description

ManagerName

GroupName

Description

ManagerName

GroupName

Description

ManagerName

Etc...

3 Replies

@PoconoNeo  Hello Neo, my approach would be to use a HT and save everything to a new var and then export it to the CSV/HTML file.

$Results = @()

Get-ADGroup -filter { Description -like "Admin Group*"} -Properties CN, Description, ManagedBy | Sort-Object "CN" | ForEach-Object {
$GroupName = $_.CN;
$Description = $_.Description;
$Manager = $_.Managedby;
if (!$Manager) {
    $Manager = 'N/A';
}

Get-ADUser -Filter * -SearchBase $Manager -Properties * {
    $GivenName = $_.GivenName;
    $Surname = $_.Surname;
    $managerName = $GivenName, $Surname;
}
if (!$ManagerName) {
    (Get-ADGroup -Filter * -SearchBase $Manager -Properties Name) | ForEach-Object {
        $managerName = $_.Name;  
    }
}



#Write-Output $Groupname, $Description, $ManagerName
  $Props = $null
  $Props = [Ordered]@{
		              GroupName   = $Groupname
                      Description = $Description
                      ManagerName = $ManagerName
                     }
  
  $Results += New-Object PSObject -Property $Props


}
$Results | Export-Csv -Path "/Path to save csv/Report.csv" -NoTypeInformation -Encoding UTF8 -Force 
$Results | ConvertTo-Html -Property GroupName,Description,ManagerName | Out-File  "/Path to save html/Report.html" 

  

Let Me know if you have further questions.

 

Regards

Erick Moreno

@Erick A. Moreno R. Thanks for that, now that it is in correct formats, it is not outputting the groups that don't have a managedby field set, would like to display those groups as N/A.

@PoconoNeo This should cover that. 

 

$Results = @()

Get-ADGroup -filter { Description -like "Admin Group*"} -Properties CN, Description, ManagedBy | Sort-Object "CN" | ForEach-Object {

$Manager = $GroupName = $Description = $null
$GroupName = $_.CN;
$Description = $_.Description;
$Manager = $_.Managedby;

If($Manager)
 {  
  Get-ADUser -Filter * -SearchBase $Manager -Properties * {
      $GivenName = $_.GivenName;
      $Surname = $_.Surname;
      $ManagerName = $GivenName, $Surname;
  }
  if (!$ManagerName) {
      (Get-ADGroup -Filter * -SearchBase $Manager -Properties Name) | ForEach-Object {
          $managerName = $_.Name;  
      }
  }
 }
Else
 {
  $ManagerName = 'N/A';
 }



#Write-Output $Groupname, $Description, $ManagerName
  $Props = $null
  $Props = [Ordered]@{
		              GroupName   = $Groupname
                      Description = $Description
                      ManagerName = $ManagerName
                     }
  
  $Results += New-Object PSObject -Property $Props


}
$Results | Export-Csv -Path "/Path to save csv/Report.csv" -NoTypeInformation -Encoding UTF8 -Force 
$Results | ConvertTo-Html -Property GroupName,Description,ManagerName | Out-File  "/Path to save html/Report.html"