Reputation: 445
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.
Upvotes: 0
Views: 442
Reputation: 445
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 :)
Upvotes: 0
Reputation: 68331
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
Upvotes: 3
Reputation: 37800
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
Upvotes: 0