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

%3CLINGO-SUB%20id%3D%22lingo-sub-1394189%22%20slang%3D%22en-US%22%3ECreate%20CSV%20%26amp%3B%20HTML%20showing%20AD%20Group%2C%20Group%20Description%2C%20ManagedBy%20(Friendly%20Name)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1394189%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EI%20need%20some%20help%20doing%20a%20search%20of%20AD%20groups%20with%20the%20Description%20starting%20with%20%22Admin%20Group*%22%20and%20get%20the%20Managedby%20friendly%20name.%20Some%20groups%20are%20managed%20by%20groups%20and%20other%20are%20by%20user.%20I've%20tried%20putting%20together%20something%20using%20other%20script%20examples%20but%2C%20I'm%20able%20to%20get%20the%20data%20but%20can't%20figure%20out%20how%20to%20export%20the%20results%20to%20CSV%20%26amp%3B%20HTML.%20Here%20is%20what%20I%20have%20so%20far%3A%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EGet-ADGroup%20-filter%20%7B%20Description%20-like%20%22Admin%20Group*%22%7D%20-Properties%20CN%2C%20Description%2C%20ManagedBy%20%7C%20Sort-Object%20%22CN%22%20%7C%20ForEach-Object%20%7B%0A%24GroupName%20%3D%20%24_.CN%3B%0A%24Description%20%3D%20%24_.Description%3B%0A%24Manager%20%3D%20%24_.Managedby%3B%0Aif%20(!%24Manager)%20%7B%0A%20%20%20%20%24Manager%20%3D%20'N%2FA'%3B%0A%7D%0A%0AGet-ADUser%20-Filter%20*%20-SearchBase%20%24Manager%20-Properties%20*%20%7B%0A%20%20%20%20%24GivenName%20%3D%20%24_.GivenName%3B%0A%20%20%20%20%24Surname%20%3D%20%24_.Surname%3B%0A%20%20%20%20%24managerName%20%3D%20%24GivenName%2C%20%24Surname%3B%0A%7D%0Aif%20(!%24ManagerName)%20%7B%0A%20%20%20%20(Get-ADGroup%20-Filter%20*%20-SearchBase%20%24Manager%20-Properties%20Name)%20%7C%20ForEach-Object%20%7B%0A%20%20%20%20%20%20%20%20%24managerName%20%3D%20%24_.Name%3B%20%20%0A%20%20%20%20%7D%0A%7D%0A%0AWrite-Output%20%24Groupname%2C%20%24Description%2C%20%24ManagerName%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20writes%20the%20output%20like%20this%20though%3A%3C%2FP%3E%3CP%3EGroupName%3C%2FP%3E%3CP%3EDescription%3C%2FP%3E%3CP%3EManagerName%3C%2FP%3E%3CP%3EGroupName%3C%2FP%3E%3CP%3EDescription%3C%2FP%3E%3CP%3EManagerName%3C%2FP%3E%3CP%3EGroupName%3C%2FP%3E%3CP%3EDescription%3C%2FP%3E%3CP%3EManagerName%3C%2FP%3E%3CP%3EEtc...%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1394189%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Eget-adgroup%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eget-aduser%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1394233%22%20slang%3D%22en-US%22%3ERe%3A%20Create%20CSV%20%26amp%3B%20HTML%20showing%20AD%20Group%2C%20Group%20Description%2C%20ManagedBy%20(Friendly%20Name)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1394233%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F669599%22%20target%3D%22_blank%22%3E%40PoconoNeo%3C%2FA%3E%26nbsp%3B%20Hello%20Neo%2C%20my%20approach%20would%20be%20to%20use%20a%20HT%20and%20save%20everything%20to%20a%20new%20var%20and%20then%20export%20it%20to%20the%20CSV%2FHTML%20file.%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24Results%20%3D%20%40()%0A%0AGet-ADGroup%20-filter%20%7B%20Description%20-like%20%22Admin%20Group*%22%7D%20-Properties%20CN%2C%20Description%2C%20ManagedBy%20%7C%20Sort-Object%20%22CN%22%20%7C%20ForEach-Object%20%7B%0A%24GroupName%20%3D%20%24_.CN%3B%0A%24Description%20%3D%20%24_.Description%3B%0A%24Manager%20%3D%20%24_.Managedby%3B%0Aif%20(!%24Manager)%20%7B%0A%20%20%20%20%24Manager%20%3D%20'N%2FA'%3B%0A%7D%0A%0AGet-ADUser%20-Filter%20*%20-SearchBase%20%24Manager%20-Properties%20*%20%7B%0A%20%20%20%20%24GivenName%20%3D%20%24_.GivenName%3B%0A%20%20%20%20%24Surname%20%3D%20%24_.Surname%3B%0A%20%20%20%20%24managerName%20%3D%20%24GivenName%2C%20%24Surname%3B%0A%7D%0Aif%20(!%24ManagerName)%20%7B%0A%20%20%20%20(Get-ADGroup%20-Filter%20*%20-SearchBase%20%24Manager%20-Properties%20Name)%20%7C%20ForEach-Object%20%7B%0A%20%20%20%20%20%20%20%20%24managerName%20%3D%20%24_.Name%3B%20%20%0A%20%20%20%20%7D%0A%7D%0A%0A%0A%0A%23Write-Output%20%24Groupname%2C%20%24Description%2C%20%24ManagerName%0A%20%20%24Props%20%3D%20%24null%0A%20%20%24Props%20%3D%20%5BOrdered%5D%40%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20GroupName%20%20%20%3D%20%24Groupname%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Description%20%3D%20%24Description%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ManagerName%20%3D%20%24ManagerName%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%0A%20%20%24Results%20%2B%3D%20New-Object%20PSObject%20-Property%20%24Props%0A%0A%0A%7D%0A%24Results%20%7C%20Export-Csv%20-Path%20%22%2FPath%20to%20save%20csv%2FReport.csv%22%20-NoTypeInformation%20-Encoding%20UTF8%20-Force%20%0A%24Results%20%7C%20ConvertTo-Html%20-Property%20GroupName%2CDescription%2CManagerName%20%7C%20Out-File%20%20%22%2FPath%20to%20save%20html%2FReport.html%22%20%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3ELet%20Me%20know%20if%20you%20have%20further%20questions.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERegards%3C%2FP%3E%3CP%3EErick%20Moreno%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1394286%22%20slang%3D%22en-US%22%3ERe%3A%20Create%20CSV%20%26amp%3B%20HTML%20showing%20AD%20Group%2C%20Group%20Description%2C%20ManagedBy%20(Friendly%20Name)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1394286%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F89674%22%20target%3D%22_blank%22%3E%40Erick%20A.%20Moreno%20R.%3C%2FA%3E%26nbsp%3BThanks%20for%20that%2C%20now%20that%20it%20is%20in%20correct%20formats%2C%20it%20is%20not%20outputting%20the%20groups%20that%20don't%20have%20a%20managedby%20field%20set%2C%20would%20like%20to%20display%20those%20groups%20as%20N%2FA.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1394329%22%20slang%3D%22en-US%22%3ERe%3A%20Create%20CSV%20%26amp%3B%20HTML%20showing%20AD%20Group%2C%20Group%20Description%2C%20ManagedBy%20(Friendly%20Name)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1394329%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F669599%22%20target%3D%22_blank%22%3E%40PoconoNeo%3C%2FA%3E%26nbsp%3BThis%20should%20cover%20that.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24Results%20%3D%20%40()%0A%0AGet-ADGroup%20-filter%20%7B%20Description%20-like%20%22Admin%20Group*%22%7D%20-Properties%20CN%2C%20Description%2C%20ManagedBy%20%7C%20Sort-Object%20%22CN%22%20%7C%20ForEach-Object%20%7B%0A%0A%24Manager%20%3D%20%24GroupName%20%3D%20%24Description%20%3D%20%24null%0A%24GroupName%20%3D%20%24_.CN%3B%0A%24Description%20%3D%20%24_.Description%3B%0A%24Manager%20%3D%20%24_.Managedby%3B%0A%0AIf(%24Manager)%0A%20%7B%20%20%0A%20%20Get-ADUser%20-Filter%20*%20-SearchBase%20%24Manager%20-Properties%20*%20%7B%0A%20%20%20%20%20%20%24GivenName%20%3D%20%24_.GivenName%3B%0A%20%20%20%20%20%20%24Surname%20%3D%20%24_.Surname%3B%0A%20%20%20%20%20%20%24ManagerName%20%3D%20%24GivenName%2C%20%24Surname%3B%0A%20%20%7D%0A%20%20if%20(!%24ManagerName)%20%7B%0A%20%20%20%20%20%20(Get-ADGroup%20-Filter%20*%20-SearchBase%20%24Manager%20-Properties%20Name)%20%7C%20ForEach-Object%20%7B%0A%20%20%20%20%20%20%20%20%20%20%24managerName%20%3D%20%24_.Name%3B%20%20%0A%20%20%20%20%20%20%7D%0A%20%20%7D%0A%20%7D%0AElse%0A%20%7B%0A%20%20%24ManagerName%20%3D%20'N%2FA'%3B%0A%20%7D%0A%0A%0A%0A%23Write-Output%20%24Groupname%2C%20%24Description%2C%20%24ManagerName%0A%20%20%24Props%20%3D%20%24null%0A%20%20%24Props%20%3D%20%5BOrdered%5D%40%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20GroupName%20%20%20%3D%20%24Groupname%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Description%20%3D%20%24Description%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ManagerName%20%3D%20%24ManagerName%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%0A%20%20%24Results%20%2B%3D%20New-Object%20PSObject%20-Property%20%24Props%0A%0A%0A%7D%0A%24Results%20%7C%20Export-Csv%20-Path%20%22%2FPath%20to%20save%20csv%2FReport.csv%22%20-NoTypeInformation%20-Encoding%20UTF8%20-Force%20%0A%24Results%20%7C%20ConvertTo-Html%20-Property%20GroupName%2CDescription%2CManagerName%20%7C%20Out-File%20%20%22%2FPath%20to%20save%20html%2FReport.html%22%20%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
New 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"