0

I have a list of Office 365 users that I want to reset the passwords for. I would like to reset the password for each user and then output the username and password to a CSV file.

Using the Set-MsolUserPassword cmdlet only returns the password, so I'm a bit stuck.

So far, this is what I have:

[Reflection.Assembly]::LoadWithPartialName("System.Web")
foreach ( $name in $names ) {
    $newPassword = $([System.Web.Security.Membership]::GeneratePassword(9,1))
    Set-MsolUserPassword -UserPrincipalName $name.UserPrincipalName -ForceChangePassword 1 -NewPassword $newPassword -TenantId $tenID
    }

This returns a long list of passwords. What I would like it to do is return a CSV file containing $name.UserPrincipalName and $newPassword.

3 Answers 3

3

One option is to add the new password as an additional property to your existing objects, and then select out the 2 properties you want for export:

[Reflection.Assembly]::LoadWithPartialName("System.Web")
foreach ( $name in $names ) {
    $newPassword = $([System.Web.Security.Membership]::GeneratePassword(9,1))
    Set-MsolUserPassword -UserPrincipalName $name.UserPrincipalName -ForceChangePassword 1 -NewPassword $newPassword -TenantId $tenID
    $name | Add-Member -MemberType NoteProperty -Name NewPassword -Value $newPassword -PassThru
    }

$names | 
 select UserPrincipalName,NewPassword |
 Export-Csv c:\somedir\somefile.csv
Sign up to request clarification or add additional context in comments.

Comments

0

Untested, but this should get you going in a productive direction:

$passwordList = @()
[Reflection.Assembly]::LoadWithPartialName("System.Web")
foreach ( $name in $names ) {

    $newPassword = $([System.Web.Security.Membership]::GeneratePassword(9,1))
    Set-MsolUserPassword -UserPrincipalName $name.UserPrincipalName -ForceChangePassword 1 -NewPassword $newPassword -TenantId $tenID
    $temp = New-Object PSCustomObject -Property @{'Name' = $name; 'Password' = $newPassword;}
    $passwordList += $temp
    }
$passwordList | Export-CSV C:\PATH\TO\File.csv -NoTypeInformation

Comments

0

I figured out a way to do this by creating a brand new table from scratch:

[Reflection.Assembly]::LoadWithPartialName("System.Web")
$PasswordTable = New-Object system.Data.DataTable "PasswordTable"
$col1 = New-Object system.Data.DataColumn UserPrincipalName,([string])
$col2 = New-Object system.Data.DataColumn NewPassword,([string])
$PasswordTable.Columns.Add($col1)
$PasswordTable.Columns.Add($col2)
foreach ( $name in $names ) {
    $newPassword = $([System.Web.Security.Membership]::GeneratePassword(10,1))
    Set-MsolUserPassword -UserPrincipalName $name.UserPrincipalName -ForceChangePassword 1 -NewPassword $newPassword [-TenantId $tenID]
    $row = $PasswordTable.NewRow()
    $row.UserPrincipalName = $name.UserPrincipalName
    $row.NewPassword = $newPassword
    $PasswordTable.Rows.Add($row)
    }
$PasswordTable | Export-Csv C:\path\to\file.csv

But honestly I prefer mjolinor's answer :)

Comments

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.