Powershell script to process exited users

I am trying to write a script to process exited employees from a text file. I've got the first part to remove memberships working, but not the rest.

Import-module activedirectory
$DisabledUsers = Get-Content "c:\filename.txt"

## Remove group memberships from user
ForEach ($User in $DisabledUsers)
        $UserGroups = Get-ADPrincipalGroupMembership -Identity $User  | ? { ($_.name -ne "Domain Users") }

    If ($UserGroups -ne $null)

        Remove-ADPrincipalGroupMembership -Identity $User -MemberOf $UserGroups -Confirm:$false

## Copy General Description as variable $desc

## Insert "Disabled - TECH - TICKET - %date" into description

## Clear manager from Organization tab
Set-ADUser ittest -manager $null

## Set account to disabled
Disable-ADAccount $User

## Move Home folder to ~disabled folder

## Copy homeMDB to variable $mail

## Paste into Notes in Telephone tabe "$desk $mail"
Who is Participating?
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.


First: All commands should be in the foreach sentence
 Second: You have forgot your test user
## Clear manager from Organization tab
Set-ADUser ittest -manager $null

Jason CrawfordTransport NinjaCommented:
Here's a script for exiting employees I wrote a while back.  It could use some polishing, but it may give you some other ideas:

Function New-EmployeeExitRequest
    Import-Module activedirectory

    $mailcheck = Get-Mailbox $ExitingEmployeeEmailAddress -ErrorAction silentlycontinue
    $alias = $ExitingEmployeeEmailAddress.split('@')[0]
    $date = Get-Date -Format s
    $batch = "$alias-$date"

    if ($mailcheck -eq $null)
        Write-Host "$ExitingEmployeeEmailAddress does not exist" -ForegroundColor red

    $forcheck = Get-Mailbox $ForwardingAddress -ErrorAction silentlycontinue

    if ($forcheck -eq $null)
        Write-Host "$ForwardingAddress does not exist" -ForegroundColor red

    $aduser = Get-ADUser -Properties * -LDAPFilter "(mail=$ExitingEmployeeEmailAddress)"
    $groups = $aduser | Get-ADPrincipalGroupMembership | ? {$_.samaccountname -notlike 'Domain Users'}

    #Removing user from all groups
    if ($groups -ne $null) 
        foreach ($group in $groups) 
            Remove-ADGroupMember $group.name -Members $aduser -Confirm:$false

    #Disabling AD account, moving AD object, configuring forwarding, and exporting mailbox
    Disable-ADAccount $aduser
    Move-ADObject -Identity $aduser -TargetPath 'OU=Former Employees/Contractors,DC=domain,DC=locall'
    Set-Mailbox $ExitingEmployeeEmailAddress -ForwardingAddress $ForwardingAddress
    New-MailboxExportRequest $ExitingEmployeeEmailAddress -FilePath \\server\shareroot\exportedpst\$alias.pst -BatchName $batch
    Write-Host "`nThe mailbox export for $ExitingEmployeeEmailAddress has begun.  The status will be checked every 2 minutes until it is complete at which point the script will continue.  Do not close this EMS window, and if you have to logoff the server, simply close the RDP window instead to allow the script to fully run.`n`n" -ForegroundColor cyan
    Start-Sleep -Seconds 10
    #Allowing for export to complete before continuing script
        $status = Get-MailboxExportRequest -BatchName $batch
        $a = 0
        if ($status.status -notlike 'Completed')
            Start-Sleep -Seconds 120
            $a = 1
            Write-Host "`nMailbox export complete - continuing script`n" -ForegroundColor cyan
    until ($a -eq 1)
    #Disabling mailbox and setting email address as alias for manager
    if ($a -eq 1)
        Set-Mailbox $ExitingEmployeeEmailAddress -ForwardingAddress $null
        Disable-Mailbox $ExitingEmployeeEmailAddress -Confirm:$false
        Set-Mailbox $ForwardingAddress -EmailAddresses @{add="$ExitingEmployeeEmailAddress"}
    Write-Host 'The script is complete.' -ForegroundColor green

Open in new window

This script will do the following:

1. Disable the AD account
2. Remove from all Distribution Groups
3. Forward email to manager if required
4. Export mailbox to a .pst

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
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
Active Directory

From novice to tech pro — start learning today.