locked
Export output as shown in PowerShell RRS feed

  • Question

  • All

    I found next code on the internet to make some kind of organizational chart based upon the manager-field in Active Directory.  This code outputs the names in Windows Powershell with indents.  I tried to output this to a text-file but the result differs.  I must have exactly the same formatting output in my text file as in the PowerShell

    Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted -Force;
    
    $count = 0
    function get-directreports{
    
    
        foreach ($user in $args){
            $spacing = "    " * $count 
            Write-Host "$spacing $((get-aduser $user).name)  "
            $count++
            get-aduser $user -Properties directreports | select -ExpandProperty directreports | foreach {get-directreports $_ } 
            $count--
        }
    }
    get-directreports "User1" 

    Tuesday, June 12, 2018 12:51 PM

All replies

  • Unfortunately what you found is a really bad example ... actually for how to do it NOT. Sorry. You should not proceed with it. Something like this could be a starting point

    function Get-DirectReports {
        [CmdletBinding()]
        [OutputType([System.String])]
        param(
    	    [Parameter(ValueFromPipeline=$true)]
    	    [ValidateNotNullOrEmpty()]
    	    [System.String[]]
    	    $Name
        )
        }
        process {
            Get-ADUser -Identity $user -Properties directreports | 
                Select-Object -Property Name,DirectReports
    }
    
    Get-DirectReports 'User1','User2' | Format-Table -AutoSize
    
    Get-DirectReports 'User1','User2' | Out-File -FilePath C:\Sample\Direct.txt
    
    Get-DirectReports 'User1','User2' | Export-Csv -Path C:\Sample\Direct.csv -Delimiter ',' -NoTypeInformation

    It shows you how to create a function with a param block and how to format and export your output.

    But actually you should stop by in the Microsoft Technet Script Library or in the Powershell Gallery. I'm pretty sure there a some scripts doing at least almost exactly what you're trying to create new  ..... ;-)  ... no need to re-invent the wheel.



    Best regards,

    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Tuesday, June 12, 2018 1:33 PM
    Tuesday, June 12, 2018 1:31 PM
  • This example outputs the entire organizational structure defined by the manager and directReports attributes into a file in csv, html, or text output. Note that indenting is used.

    https://gallery.technet.microsoft.com/Document-Active-Directory-47486cf5

    Edit: And if you are interested in making an organizational chart, this Wiki shows how the script I linked works, and has a link describing how to use the output file from this script to create an organization chart in Visio. However, the chart gets unwieldy if the organization is large.

    https://social.technet.microsoft.com/wiki/contents/articles/30104.document-your-active-directory-organization.aspx


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)


    Tuesday, June 12, 2018 2:04 PM