Organizational Chart using PowerShell

My accounting department has asked for a simple chart that will list the managers and who their direct reports are.
First I used Visio to create a flowchart but it would be much simpler for them to just have a basic text file or csv.
Manager Name - Department
Direct Report 1
Direct Report 2

(Need something here to separate to make it visually obvious there is a new manager, a simple newline or character to break it up would work fine.)

Manager Name 2 - Department
Direct Report 1
Direct Report 2

I’ve got a basic script working but I’m having trouble with two things.

  1. I only want the managers name and direct reports name. I’m getting a lot of extra information that’s making it hard to read.(things like OUs,O, DC, etc.)
  2. I don’t have any space between direct reports and managers so it’s hard to tell who is a manager or a direct report.

Here is the script i’m using

get-aduser -Properties directreports -filter * | ?{$_.directreports.count
-ne 0} | select name, directreports | sort name | ft |export-csv h:\orgChart.csv

I’m also getting a lot of string errors “-ne” is not recognized even though it still seems to work.

Any suggestions for spacing between managers and removing all the OUs for a cleaner output?
Thank you!
-Chris Blum

You’re probably running into trouble because you’re trying to do this all in one line. Consider breaking this out into more of a script. You’ll get more control, and more opportunity to tweak the data into a form you want. It’s also helpful if you can show a snippet of your output, so that we can more easily see what you don’t like about what you’re getting.

Also, read “The Big Book of PowerShell Gotchas.” You’re running into something very common with the Format-Table command. You can’t export its output.