Disable AD disabled CS users powershell script

I’ve made a script to disable AD disabled users from Lync. The script pulls all AD disabled users and checks if they are disabled for Lync as well. If not, they will be. Optional output to screen and/or file.

Download Disable-AdDisabledCsUsers.zip or copy the sourcecode:

 #####################################################################################
 # Disable-AdDisabledCsUsers.ps1
 #
 # Pulls all AD disabled users from AD and disables them for Lync as well
 #
 # Can optionally write logs to file or screen using -verbose and/or -logFile inputs
 #
 # eg.
 #
 # .Disable-AdDisabledCsUsers.ps1 -verbose $true -logFile "c:logfile.log"
 #
 #
 #
 # Written by Tom-Inge Larsen (codesalot.com)
 #
 ####################################################################################
 param($verbose,$logFile)
Import-Module active*</code></code>if ($logFile -ne "") {
 $logoutput = [System.IO.StreamWriter] $logFile
 $logoutput.WriteLine("AD disabled users that was Lync disabled:")
 }

$disabledADusers = Search-ADAccount -AccountDisabled -UsersOnly | Select-Object userprincipalname

$disabledADusers | foreach-object {
 $identity = $_.userprincipalname
 $csuser = Get-CsUser -Identity $identity -ErrorAction SilentlyContinue | Select-Object Enabled

if ($csuser.enabled -eq $true) {
 Disable-CsUser -Identity $identity
 if ($verbose -eq $true) {
 Write-Host "AD disabled user" $identity "is now disabled for Lync as well"
 }
 if ($logFile -ne "") {
 $logoutput.WriteLine($identity)
 }
 }
 }

if ($logFile -ne "") {
 $logoutput.close()
 if ($verbose -eq $true) {
 Write-Host $logFile "written."
 }
 }