public/Export-Users.ps1

Function Export-Users {

Param(
    [int]$Days
) # end param


# Get path friendly date (dd-mm-yy)
$Today = Get-Date -UFormat %d-%m-%y

# Get ALL users in domain
$AllUsers = Get-ADUser -filter * -Properties *

# Filter uses only if Days variable has been assigned
if ($Days) {

    # Filter to those logged in within last 90 days
    $AllUsers = $AllUsers | Where-Object  lastlogondate -gt ((Get-Date).AddDays(-$Days))
    }

# Output to a CSV file with firstname, lastname, username, and also OU they were in
$AllUsers |
select givenname, surname, samaccountname, distinguishedname |
ConvertTo-Csv -NoTypeInformation |
Out-File -FilePath "$Home\Desktop\User_Export-$Today.csv"

}