0

I have a PowerShell script to bulk create users in AD from a CSV file. I want to expand on it by creating a separate CSV file that contains the details of any accounts that have failed to create. To do this, I'm creating an array containing the original details that can be then exported to CSV at the end of the script. My code for testing is as follows:

Function AddToCSV($reason)
{
    $object = New-Object -TypeName PSObject
    $object | Add-Member -Name "Firstname" -MemberType NoteProperty -Value $UserFirstName
    $object | Add-Member -Name "Lastname" -MemberType NoteProperty -Value $UserLastName
    $object | Add-Member -Name "SAM" -MemberType NoteProperty -Value $Sam
    $object | Add-Member -Name "Password" -MemberType NoteProperty -Value $Password
    $object | Add-Member -Name "Description" -MemberType NoteProperty -Value $Description
    $object | Add-Member -Name "AdGroups" -MemberType NoteProperty -Value $AdGroups         
    $object | Add-Member -Name "Reason" -MemberType NoteProperty -Value $reason
    $global:failArray += $object
}

$failArray = @()
$array = ("One","Two","Three")
foreach ($item in $array)
{
    $UserFirstName = "Test"
    $UserLastName = "Test"
    $Sam = "TEST.ACCOUNT"
    $Password = "P4ssword"
    $Description = "Test User"
    $AdGroups = "Group1;Group2"
    AddToCSV "Account already exists"
}

$failArray | Format-Table

Items get added to the array successfully, but when I try to display the array with the Format-Table, it outputs like so:

@{Firstname=Test; Lastname=Test; SAM=TEST.ACCOUNT; Password=P4ssword; Description=Test User; AdGroups=Group1;Group2; Reason=Account already exists}@{Firstname=Test; Lastname=Test; SAM=TEST.AC
COUNT; Password=P4ssword; Description=Test User; AdGroups=Group1;Group2; Reason=Account already exists}@{Firstname=Test; Lastname=Test; SAM=TEST.ACCOUNT; Password=P4ssword; Description=Test U
ser; AdGroups=Group1;Group2; Reason=Account already exists}

Trying to export to a CSV with $failArray | Export-CSV -PAth "C:\Temp\Test.csv" only results in this output:

Screenshot

How can I get the array to output in a table-like format, or into a usable CSV file?

1 Answer 1

1

You don't need a function for that. Just collect the failed ones in an array of PSObjects, not as separate string items.

Something like this:

$array = ("One","Two","Three")
$failArray = foreach ($item in $array) {
    [PsCustomObject]@{
        'UserFirstName' = 'Test'
        'UserLastName'  = 'Test'
        'Sam'           = 'TEST.ACCOUNT ({0})' -f $item
        'Password'      = 'P4ssword'
        'Description'   = 'Test User'
        'AdGroups'      = 'Group1;Group2'
        'Reason'        = 'Account already exists'
    }
}

# output on screen
$failArray | Format-Table

# output to CSV
$failArray | Export-Csv -Path 'C:\Temp\Test.csv' -NoTypeInformation

P.S. The -NoTypeInformation prevents the csv to have a first line of #TYPE blah.blah.blah

Output on screen

UserFirstName UserLastName Sam                  Password Description AdGroups      Reason                
------------- ------------ ---                  -------- ----------- --------      ------                
Test          Test         TEST.ACCOUNT (One)   P4ssword Test User   Group1;Group2 Account already exists
Test          Test         TEST.ACCOUNT (Two)   P4ssword Test User   Group1;Group2 Account already exists
Test          Test         TEST.ACCOUNT (Three) P4ssword Test User   Group1;Group2 Account already exists
Sign up to request clarification or add additional context in comments.

1 Comment

That's great, thanks @Theo! I've worked your example into my code and it is now doing what I want!

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.