Calling me a Powershell beginner would be giving me too much credit. I'm trying to read different Microsoft articles and StackOverflow threads, and they've been extremely helpful, but I've hit a point that's got me stumped. I'm trying to run through our AD OUs, checking every user in each OU, spitting out any user whose password is going to expire within 14~ days. I have a successful script that does just that, but now I'm trying to add extra columns into the CSV it spits out so I can have additional data to go off of like email address, password expiry date, etc. After much research this is what I've come up with, but it's spitting the two object properties (Email and PWExpires) into the same CSV column.
Any assistance is greatly appreciated!
$CutOffDate = (get-date).AddDays(-166).Date
$pwExpDate = $User.PasswordLastSet
$MaxCutOffDate = (get-date).AddDays(-365).Date
$pwExpList = [System.Collections.Generic.List[System.Object]]::new()
$OUs=
#deleted for posting
foreach($OU in $OUs) {
$Users = Get-ADUser -filter * -SearchBase $OU -properties *
foreach($User in $Users) {
if(($User.PasswordLastSet -lt $CutOffDate) -and ($User.PasswordLastSet-gt $MaxCutOffDate) -and ($User.EmailAddress -notlike 'noreply*') -and ($User.EmailAddress -notlike '')) {
$userObj = [PSCustomObject]@{
Email = $User.EmailAddress
PwExpires = $User.PasswordLastSet.AddDays(180)
}
$pwExpList.Add($userObj)
#$pwExpList.Add($User.PasswordLastSet.AddDays(180))
}
}
}
$pwExpList | Out-File -FilePath G:\pwExpList2.csv
Export-Csvinstead ofOut-File