2

Trying to export 4 objects from Ad to a fixed-width txt file with no header.

I need the following columns to be the width that follows.

Employee ID    10 
Work Phone      10
Work Phone Extension    5
Work Email Address      50
User ID             20

The following gives me the best output, but doesn't size the columns the way I need. I have been digging around, and think what I need is a bit beyond what I'm comfortable with.

I'm not sure if i need to export with export-csv and then import that into reformat or if I can do out-file directly.

$DateTime = Get-Date -f "yyyyMMdd"

#// Set CSV file name
$CSVFile = "d:\scripts\workday\int002_"+$DateTime+".txt"

Get-ADGroup -Filter {(name -like "*Group Name*")} `
    | Get-ADGroupMember -Recursive | Where { $_.objectClass -eq "user" } `
    | Get-ADUser -properties * | where {$_.enabled -eq $true} `
    | select employeeid,telephoneNumber,mail,sAMAccountName  -unique | FT employeeid,telephoneNumber,mail,sAMAccountName -hidetableheaders -autosize | out-file $CSVFile

Sample Output:

8855      2122445710     [email protected]                 michalsenm 
4
  • Please note that the edit UI has built-in formatting which you can use to fix the strange formatting the code has in this question. Commented Dec 18, 2018 at 18:18
  • What happened with the Work Phone Extension in your sample output? Commented Dec 18, 2018 at 18:44
  • Sorry, don't need the work phone extension extracted. I forgot to remove it from the exported information. Commented Dec 18, 2018 at 19:27
  • Why do you require fixed width output? Wouldn't a CSV output file be easier? Commented Dec 19, 2018 at 20:08

1 Answer 1

3

You might need to do it manually...

$result = foreach($user in $users) {
    $user.employeeid.PadRight(10),
    $user.telephoneNumber.PadRight(10),
    $user.mail.PadRight(50),
    $user.sAMAccountName.PadRight(20) -join ' '
}

$result | Out-File $CSVFile

A revised version that also works if the property is not a string:

$result = foreach($user in $users) {
    '{0,-10}{1,-10}{2,-50}{3,-20}' -f
        $user.employeeid,
        $user.telephoneNumber,
        $user.mail,
        $user.sAMAccountName
}

$result | Out-File $CSVFile
Sign up to request clarification or add additional context in comments.

8 Comments

Padding wouldn't shorten possibly too long entries, this requieres also a .Substring(0,width) and padding is IMO easier done with a -format operator for all properties. => "{0,10}{1,10}{2,5}{3,-50}{4,-20}" -f $user.employeeid, ...
I'm not sure he wants it to be shortened. A shortened email is no good.
Well shortened to the width he provides, a fixed width record format broken seems even less usefull.
Shouldn't need anything shortened or truncated. Everything I output should fit in each column.
After looking, the PadRight may be all I need. Just need to play with the code to get it working properly. Currently it is outputting an empty file.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.