i am the funny
 

 I AM THE FUNNY!!!

Ok, so this is a simple script that I made to keep checking active directory to find when a user exists and make sure that they are active. The driver behind this script is because we don't create accounts directly in active directory, it is done by task sequences, so you need to wait for it to occur. And waiting and checking manually is boring! Enjoy...

NOTE: I addded some updates to this script:

  • Changed it to search against sAMAccountName rather than CN.
  • Made it so it will exit the loop and pause before exiting the script.
  • Imports the active directory module, cause you kinda need it for the script to work...
  • Import-Module ActiveDirectory
    
    $userlist = @()
    
    $userlist = "WoodR","adm.WoodR","sup.WoodR"
    
    # Getting the number of users you have put in
    $numberUsers = $userlist.Count
    
    $x=0
    
    # This is set up to loop through 100 times, or until all users exist
    do {
        cls
        $countingUsers=0
        $x++
        write-host "Search $x of 100`n"
        foreach($user in $userlist){
            # checks to see if the user exists checking sAMAccountName
            $b = Get-ADUser -ldapfilter "(&(ObjectCategory=User)(sAMAccountName=$user))"
            if ($b -eq $null){
                write-host "User $user has not been created yet." -ForegroundColor Red
                Write-Host
            }else{
                # user exist so checking state
                $a = Get-aduser -ldapfilter "(&(ObjectCategory=User)(sAMAccountName=$user))" -properties useraccountcontrol, displayName | select name, useraccountcontrol, displayName
                Switch ($a.useraccountcontrol){
                "512" {
                    # Enabled, Normal Account
                    Write-Host -NoNewline $a.name "-" $a.displayName "is "  -ForegroundColor White
                    Write-Host -NoNewline $a.useraccountcontrol "Enabled, Normal Account " -ForegroundColor Green
                    Write-Host
                    $countingUsers++
                    }
                "544" {
                    # Enabled, Password Not Required
                    Write-Host -NoNewline $a.name " - " $a.displayName " is "  -ForegroundColor White
                    Write-Host -NoNewline $a.useraccountcontrol " - Enabled, Password Not Required " -ForegroundColor Green
                    Write-Host
                    $countingUsers++
                    }
                "66048" {
                    # Enabled, Password Doesn’t Expire
                    Write-Host -NoNewline $a.name " - " $a.displayName " is "  -ForegroundColor White
                    Write-Host -NoNewline $a.useraccountcontrol " - Enabled, Password Doesn’t Expire " -ForegroundColor Green
                    Write-Host
                    $countingUsers++
                    }
                "262656" {
                    # Enabled, Smartcard Required
                    Write-Host -NoNewline $a.name " - " $a.displayName " is " -ForegroundColor White
                    Write-Host -NoNewline $a.useraccountcontrol " -  Enabled, Smartcard Required " -ForegroundColor Green
                    Write-Host
                    $countingUsers++
                    }
                "8388608" {
                    # Enabled, Smartcard Required
                    Write-Host -NoNewline $a.name " - " $a.displayName " is " -ForegroundColor White
                    Write-Host -NoNewline $a.useraccountcontrol " - Password Expired " -ForegroundColor Yellow
                    Write-Host
                    }
                Default {
                    Write-Host -NoNewline $a.name " - " $a.displayName " is " -ForegroundColor White
                    Write-Host -NoNewline $a.useraccountcontrol " - Account disabled or unknown state " -ForegroundColor Red
                    Write-Host
                    }
                }
            }
            $a = ""
            $b = ""
        }
         # Sets x to 100 so that it exits if all users exist
        If($countingUsers -eq $numberUsers){
            $x=100
            Write-host "`nAll users created and enabled.`n`nExiting now."
            start-sleep 2
        }Else{
            Start-Sleep -Seconds 30
        }
    
     }until($x -eq 100)
    
     Pause