简体   繁体   中英

Active Directory Powershell to fixed width txt file

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     xxxry.michalsen@companydomain.com                 michalsenm 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM