Description

Returns all Office 365 users, color codes the ones modified within the last 7 days, and exports the results as a CSV file.
*Note: does not color code the exported document

Source Code

#connects to Office 365
$msolcred = Get-credential -UserName "email@youraddress.com" -Message "Please type in your password"
connect-msolservice -credential $msolcred

#connects and imports session
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $msolcred -Authentication Basic –AllowRedirection
Import-PSSession $Session

#clears results variable
$results = @()

#sets variables and selects all users from Office 365
$objUsers = Get-Mailbox -ResultSize Unlimited | Select UserPrincipalName 
$today = Get-Date
$lastweek = (Get-Date).AddDays(-7)
 
#begin loop
Foreach ($objUser in $objUsers) {

#retrieves user properties
$objUserMailbox = Get-User -Identity $($objUser.UserPrincipalName) | Select identity, whenCreated, whenChanged, DisplayName, SignInName

#creates document
$results += New-Object PSCustomObject -Property @{ 
        DisplayName = $strDisplayName
        SignInName = $strUserSignInName
        whenCreated = $strWhenCreated
        whenChanged = $strWhenChanged
        }

#more variables linking to the users' attributes
$strUserPrincipalName = $objUser.UserPrincipalName
$strWhenCreated = $objUserMailbox.whenCreated
$strWhenChanged = $objUserMailbox.whenChanged
$strDisplayName = $objUserMailbox.DisplayName
$strUserSignInName = $objUserMailbox.SignInName

#if user was modified in the last 7 days color output red, if not then green
If ($strWhenChanged -gt $lastweek -and $strWhenChanged -lt $today) {
                Write-Host "$strDisplayName : $strUserPrincipalName : $strWhenCreated : $strWhenChanged" -foregroundcolor "Red"
                }

                Else {
                Write-Host "$strDisplayName : $strUserPrincipalName : $strWhenCreated : $strWhenChanged" -foregroundcolor "Green"
                }

}

#exports document as CSV
$results | Export-Csv -Path C:\SamplePath\O365_LastModified.csv –notypeinformation -Delimiter ","

#disconnects session
Remove-PSSession $Session