Password age for users in Office 365

Short script that gets all the users in your Office 365 tenant, and finds the password age.
Creates a list with UPN and password age, then sort by oldest password.

Also finds the average password age for your tenant.


Connect-MsolService

#Creates a array to put data into.
$array=@()

#Gets all users in your tenant.
$users = Get-MsolUser

#Foreach loop to get UPN and Password age for each user.
foreach($user in $users) {

#Gets the date for when the password was changed and sets corect format.
$date = [DateTime]$user.LastPasswordChangeTimestamp

#Gets todays date.
$today = Get-Date

#Finds how many days since the password has been changed.
$lastpasswordchange = New-TimeSpan -Start $date -End $today

#Add data to array
$object = [PSCustomObject]@{
    UPN = $user.UserPrincipalName
    PasswordAge = $lastpasswordchange.Days

}
$array += $object

}

#Function to find avrage number
Function Average($array2)
{
    $RunningTotal = 0;
    foreach($i in $array2){
        $RunningTotal += $i
    }
    return ([decimal]($RunningTotal) / [decimal]($array2.Length));
}

#Finds the avrage password age for your tenant.
$APassword = Average($array.passwordage)

#Rounds the avrage password age to a whole number.
$APassword2 = [math]::Round($APassword) 

"Average password age is $Apassword2 days."

"List of password age per user:"

#Sorts list of UPN and PasswordAge starting with the oldest password.
$array | Sort-Object -descending PasswordAge

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s