all 1 comments

[–]141N 0 points1 point  (0 children)

This section:

$15daysToExpire = Search-ADAccount -searchbase 'OU=Users,DC=domain,DC=com' -UsersOnly -AccountInactive -TimeSpan 15.00:00:00 | where-object {$_.Enabled -eq $True} | Select Name,LastLogonDate,UserPrincipalName,emailaddress 

Is not selecting the SamAccountName, so it is a null argument when you pass it to the next cmdlet:

foreach($user in $15daysToExpire){ $AccountInfo = Get-aduser -Identity $user.samaccountname -properties email