Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude disable user Get-aduser

Tags:

powershell

I want to exclude disabled user from this script but can't seem to find how i try the -exclude with no luck.

if you have a better way to do it in open to suggestion too

import-module ActiveDirectory;

$maxPasswordAgeTimeSpan = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge

Get-ADUser -filter * -properties PasswordLastSet, PasswordExpired, PasswordNeverExpires, EmailAddress, GivenName | foreach {

   $today=get-date
   $UserName=$_.GivenName
   $Email=$_.EmailAddress

   if (!$_.PasswordExpired -and !$_.PasswordNeverExpires) {

       $ExpiryDate=$_.PasswordLastSet + $maxPasswordAgeTimeSpan
       $DaysLeft=($ExpiryDate-$today).days

       if ($DaysLeft -lt 10 -and $DaysLeft -gt 0){

        $WarnMsg = "
<p style='font-family:arial'>Bonjour $UserName,</p>
<p style='font-family:arial'>Votre mot de passe va expirer dans $DaysLeft jours, S.V.P. changer votre mot de passe.</p>
<p style='font-family:arial'>Merci.</p>"

$enc  = New-Object System.Text.utf8encoding
ForEach ($email in $_.EmailAddress) { 
send-mailmessage -to test@test -from [email protected] -Subject "Votre mot de passe va expirer dans $DaysLeft jours" -body $WarnMsg  -smtpserver x.x.x.x -BodyAsHtml -Encoding $enc }

    }

   }
}
like image 341
Bonneau21 Avatar asked Jul 13 '26 11:07

Bonneau21


1 Answers

Just change your filter at Get-AdUser from * to 'enabled -eq "true"'

Get-ADUser -filter 'enabled -eq "true"' -properties ...
like image 147
restless1987 Avatar answered Jul 20 '26 21:07

restless1987



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!