export results

hello there …
i have this simple script

$NewPassword = -join ((97..122) + (48..57) | Get-Random -Count 10 | % {[char]$_})
Import-Csv D:\a.csv | foreach { New-ADUser -Name $_.Name -Enabled $true `
-AccountPassword(ConvertTo-SecureString -AsPlainText $NewPassword -Force )` -SamAccountName $_.SamAccountName` -Path $OU } 

the csv column contains three columns … column1 = name … column2 = samaccountname … column3 = password
the password column in empty…
how can i export the password the generated password into the third column ? if its not possible … how can i export the results into another csv file with the generated password ?
thank you


you just read from the CSV file, but you have to write to the csv file.

My suggest: Generate the passwords, put them in the CSV-File after this Import the CSV Files and and create the ADuser


$NewPassword = -join ((97..122) + (48..57) | Get-Random -Count 10 | % {[char]$_})
$csv = Import-Csv d:\a.csv
$csv | foreach { New-ADUser -Name $_.Name -Enabled $true `
-AccountPassword(ConvertTo-SecureString -AsPlainText $NewPassword -Force )` -SamAccountName $_.SamAccountName` -Path $OU }
$csv | Select-Object *,@{'Name'='Password';'Expression'={$NewPassword}} | Export-Csv -Path d:\b.csv -NoTypeInformation -Force

this did the job perfectly … it exported the results to a new csv file with the generated password .

This is also a possible way.

Cheers mate.

Something like this should work, but it is untested.

$csv = Import-Csv D:\a.csv
foreach ($u in $csv) {
	$u.Password = -join ((97 .. 122) + (48 .. 57) | Get-Random -Count 10 | % { [char]$_ })
	Try {
		New-ADUser -Name $u.Name -Enabled $true -AccountPassword (ConvertTo-SecureString -AsPlainText $u.Password -Force)` -SamAccountName $u.SamAccountName` -Path $OU
	} Catch {
		Write-Error "$u.Name failed."
		$u.Password = ""
	$csv | Export-Csv d:\b.csv -NoTypeInformation

You can move the export outside of the loop if your input file is huge, I just like to make sure it updates after each loop. If any of the users fail, the password will be blank and you’ll be able to restart the script using the updated file after you fix the problem.

well thats actually a good idea …
but i’m getting errors

Exception setting "Password": "The property 'Password' cannot be found on this object. Verify that the property exists and can be set."

theres no such thing as


ill work on it !!!

Here is an example of using a calculated expression and splatting to make some clean code:

#Calculated Expression
$users = Import-CSV D:\a.csv | 
         Select *,
         @{Name='Password';Expression={-join ((97..122) + (48..57) | Get-Random -Count 10 | % {[char]$_})}}
foreach ($user in $users) {
    $userParam = @{
        Name            = $user.Name 
        Enabled         = $true
        AccountPassword = (ConvertTo-SecureString -AsPlainText $user.Password -Force )
        SamAccountName  = $user.SamAccountName
        Path            = "OU=TEST,OU=TEST,DC=TEST,DC=TEST,DC=TEST,DC=TEST"
    New-ADUser @userParam

One would need to replace

$u.Password = -join ((97 .. 122) + (48 .. 57) | Get-Random -Count 10 | % { [char]$_ })


$u | Add-Member -MemberType NoteProperty -Name Password -Value (-join ((97 .. 122) + (48 .. 57) | Get-Random -Count 10 | % { [char]$_ }))


$u | Add-Member -MemberType NoteProperty -Name Password -Value ([System.Web.Security.Membership]::GeneratePassword(10, 2))

because $u is a PSCustomObject not a Hashtable.

I hope that helps.

He said the column was there but empty, so shouldn’t need to add it, if that’s true.

$csv|ft -auto

a b c
- - -
1 2

$csv|ft -auto

a b c
- - -
1 2 3

well Ron … you’re right … in the source CSV i did not have a column named “PASSWORD” since my first script created it in the exported CSV … and i tried to create two users with the same name to see what it does if it fails it gave this error

New-ADUser : The specified account already exists

and in the exported csv it gave the same password for both rows…
i tried it with ur twist after i added the column Password in the source CSV and it did exactly what u said it would do … it gave a blank cell for the password
well thank you all this is very helpful actually .