7,023 questions
Hey Kendra.
I believe you are looking for this:
# Get current date
$currentDate = Get-Date
# Get all users from the domain
$users = Get-ADUser -Filter *
# Create an array to store the filtered users
$filteredUsers = @()
# Loop through each user
foreach ($user in $users) {
# Check if the user account is active
if ($user.Enabled -eq $true) {
# Check if the last logon date is more than 6 months ago
$lastLogon = [DateTime]::FromFileTime($user.LastLogonTimestamp)
$monthsAgo = ($currentDate - $lastLogon).TotalDays / 30
if ($monthsAgo -ge 6) {
# Add the user to the filtered list
$filteredUsers += New-Object PSObject -Property @{
'Username' = $user.SamAccountName
'Last Logon' = $lastLogon
}
}
}
}
# Export the filtered users to a CSV file
$filteredUsers | Export-Csv -Path "FilteredUsers.csv" -NoTypeInformation