Import Bulk users using IMPORT-CSV, export results to file

So i can successfully import users to AD using the syntax below, but while it is running, i see 'errors' on accounts that already exists in AD. How do i get it to export those duplicates/ errors to a txt/csv file while its importing users?

import-csv ASI_Import.csv | foreach {new-ADuser -Use
rPrincipalName $_.userprincipalname -SamAccountName $_.samaccountname -Name $_.n
ame -DisplayName $_.displayname -GivenName $_.givenname -Surname $_.surname -Des
cription $_.description -Department $_.department -AccountPassword (ConvertTo-Se
cureString -AsPlainText "Password" -Force) -Enabled $false -Path 'OU=Import,DC=
ASI,DC=Local'}

I know i can change the layout size by increasing the height on buffer size, but would be nice to see the results in a txt file.

Any help would be appreciated!

Thanks,

JSS

September 5th, 2013 4:55pm

Hi,

Have you tried piping to Out-File? If that isn't working, take a look at Start-Transcript and Stop-Transcript.

Free Windows Admin Tool Kit Click here and download it now
September 5th, 2013 4:58pm

No havent tried either. I can try Out-File, but dont know about Start-Transcript & Stop. How would use that to output the results?
September 5th, 2013 5:01pm

Put Start-Transcript at the top of your script and Stop-Transcript at the bottom. Here's the syntax:

http://ss64.com/ps/start-transcript.html

http://ss64.com/ps/stop-transcript.html

This will log everything though, so it may not be exactly what you're looking for.

Free Windows Admin Tool Kit Click here and download it now
September 5th, 2013 5:11pm

Thanks Mike! That kind of does it, but as you said, not exactly what i was looking for. 
September 6th, 2013 9:08am

I usually use the errorvariable common paramater of the cmdlet to resolve this case

import-csv ASI_Import.csv | foreach {
	new-ADuser -UserPrincipalName $_.userprincipalname -SamAccountName $_.samaccountname -Name $_.name -DisplayName $_.displayname -GivenName $_.givenname -Surname $_.surname -Description $_.description -Department $_.department -AccountPassword (ConvertTo-SecureString -AsPlainText "Password" -Force) -Enabled $false -Path 'OU=Import,DC=ASI,DC=Local' -errorvariable ErrCreate
	if ($ErrCreate) {
	$username = $_.samaccountname
	$log += "Error creating $username, the exeption was: `r`n"
	$log += $ErrCreate
	$ErrCreate = $null
	}
}
$log | out-file c:\whatever.txt


Free Windows Admin Tool Kit Click here and download it now
September 6th, 2013 9:17am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics