Powershell script to process exited users

davidthegnome2003
davidthegnome2003 used Ask the Experts™
on
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"
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
hello

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


Dan
Commented:
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
{    
    param(
    [parameter(position=0,mandatory=$true)]
    [string]$ExitingEmployeeEmailAddress,
    
    [parameter(position=1,mandatory=$false)]
    [string]$ForwardingAddress
    )
    
    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
        Break
    }

    $forcheck = Get-Mailbox $ForwardingAddress -ErrorAction silentlycontinue

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

    $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
    do
    {
        $status = Get-MailboxExportRequest -BatchName $batch
        $a = 0
        
        if ($status.status -notlike 'Completed')
        {
            Start-Sleep -Seconds 120
        }
        else
        {
            $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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial