PS isn't moving disable users accounts but moving disabled computer accounts

Hey Experts!  I have two PS scripts that check for accounts that haven't checked into AD for 60 days and then move them into a particular OU for both PC's and users.  However, the script for users isn't working.  I'm including both PS scripts along with the error message captured in the log file.  Any help on identifying on why the user accounts aren't being moved would be appreciated.

Working PS script for PC's
Import-Module ActiveDirectory

$logPath = "C:\Tools\Logs\_Stale_Accounts\Stale_PCs_"  #directory to log output
$Date = Get-Date
$TodaysLog = $logPath + $Date.ToString('yyyy_MM-dd_HHmm') + ".log"
$FilterDate = $Date.AddDays(-49)

Function WriteLog($message) {
    $message = (Get-Date).ToShortTimeString() + " - " + $message
    Add-Content -Path $TodaysLog -Value $message
}

$DisabledOU = "OU=Computers,OU=Inactive,OU=A,OU=B,DC=C,DC=D,DC=E,DC=F"

$StaleComputers = Get-ADComputer -SearchBase "OU=Depts,OU=A,OU=B,DC=C,DC=D,DC=E,DC=F" -Filter * -Properties DistinguishedName, SAMAccountName, lastLogonTimeStamp |
	Where-Object {[DateTime]::FromFileTime($_.lastLogonTimeStamp) -lt $FilterDate}

If ($StaleComputers) {
	ForEach ($Computer in $StaleComputers) {
		Try {
			Disable-ADAccount -Identity $Computer.DistinguishedName -ErrorAction Stop
			WriteLog -Message "Disabled $($Computer.SAMAccountName)"
		} Catch {
			WriteLog -Message "Failed to disable $($Computer.SAMAccountName): $($_.Exception.Message)"
		}
    
		Try {
			Move-ADObject -Identity $Computer.DistinguishedName -TargetPath $DisabledOU -ErrorAction Stop
			WriteLog -Message "Moved $($Computer.SAMAccountName) to $($DisabledOU)"
		} Catch {
			WriteLog -Message "Failed to move $($Computer.SAMAccountName) to $($DisabledOU): $($_.Exception.Message)"
		}
	}
} Else {
	WriteLog -Message "No stale computer accounts found."
}

Open in new window



Script not working (for users)
Import-Module ActiveDirectory

$logPath = "C:\Tools\Logs\_Stale_Accounts\Stale_Users_"  #directory to log output
$Date = Get-Date
$TodaysLog = $logPath + $Date.ToString('yyyy_MM-dd_HHmm') + ".log"
$FilterDate = $Date.AddDays(-49)

Function WriteLog($message) {
    $message = (Get-Date).ToShortTimeString() + " - " + $message
    Add-Content -Path $TodaysLog -Value $message
}

$disabledOU = "OU=Users,OU=Inactive,OU=A,OU=B,DC=C,DC=D,DC=E,DC=F"

$StaleUsers = Get-ADUser -SearchBase "OU=Depts,OU=A,OU=B,DC=C,DC=D,DC=E,DC=F" -Filter * -Properties SAMAccountName, lastLogonTimeStamp |
    Where-Object {[DateTime]::FromFileTime($_.lastLogonTimeStamp) -lt $FilterDate} 

If ($StaleUsers) {
    ForEach ($User in $StaleUsers) {
        try {
            Disable-ADAccount -Identity $User.SAMAccountName -ErrorAction Stop
            WriteLog -Message "Disabled $($User.SAMAccountName)"
        } catch {
            WriteLog -Message "Failed to disable $($User.SAMAccountName): $($_.Exception.Message)"
        }
    
        try {
            Move-ADObject -Identity $User.SAMAccountName -TargetPath $disabledOU -ErrorAction Stop
            WriteLog -Message "Moved $($User.SAMAccountName) to $disabledOU)"
        } catch {
            WriteLog -Message "Failed to move $($User.SAMAccountName) to $($disabledOU): $($_.Exception.Message)"
        }
    }
} Else {
	WriteLog -Message "No stale computer accounts found."
}

Open in new window


Error log
2:40 PM - Disabled user.k
2:40 PM - Failed to move user.k to OU=Users,OU=Inactive,OU=A,OU=B,DC=C,DC=D,DC=E,DC=F: Cannot find an object with identity: 'user.k' under: 'DC=C,DC=D,DC=E,DC=F'.
2:40 PM - Disabled user.V
2:40 PM - Failed to move user.V to OU=Users,OU=Inactive,OU=A,OU=B,DC=C,DC=D,DC=E,DC=F: Cannot find an object with identity: 'user.V' under: 'DC=C,DC=D,DC=E,DC=F'.
2:40 PM - Disabled user.T
2:40 PM - Failed to move user.T to OU=Users,OU=Inactive,OU=A,OU=B,DC=C,DC=D,DC=E,DC=F: Cannot find an object with identity: 'user.T' under: 'DC=C,DC=D,DC=E,DC=F'.
LVL 9
samiam41Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Try it with DistinguishedName instead of SamAccountName in the cmdlets:
Import-Module ActiveDirectory

$logPath = "C:\Tools\Logs\_Stale_Accounts\Stale_Users_"  #directory to log output
$Date = Get-Date
$TodaysLog = $logPath + $Date.ToString('yyyy_MM-dd_HHmm') + ".log"
$FilterDate = $Date.AddDays(-49)

Function WriteLog($message) {
    $message = (Get-Date).ToShortTimeString() + " - " + $message
    Add-Content -Path $TodaysLog -Value $message
}

$disabledOU = "OU=Users,OU=Inactive,OU=A,OU=B,DC=C,DC=D,DC=E,DC=F"

$StaleUsers = Get-ADUser -SearchBase "OU=Depts,OU=A,OU=B,DC=C,DC=D,DC=E,DC=F" -Filter * -Properties SAMAccountName, lastLogonTimeStamp |
    Where-Object {[DateTime]::FromFileTime($_.lastLogonTimeStamp) -lt $FilterDate} 

If ($StaleUsers) {
    ForEach ($User in $StaleUsers) {
        try {
            Disable-ADAccount -Identity $User.DistinguishedName -ErrorAction Stop
            WriteLog -Message "Disabled $($User.SAMAccountName)"
        } catch {
            WriteLog -Message "Failed to disable $($User.SAMAccountName): $($_.Exception.Message)"
        }
    
        try {
            Move-ADObject -Identity $User.DistinguishedName -TargetPath $disabledOU -ErrorAction Stop
            WriteLog -Message "Moved $($User.SAMAccountName) to $disabledOU)"
        } catch {
            WriteLog -Message "Failed to move $($User.SAMAccountName) to $($disabledOU): $($_.Exception.Message)"
        }
    }
} Else {
	WriteLog -Message "No stale computer accounts found."
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
samiam41Author Commented:
Genius!!  Thank you oBdA
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.