Deactivate multiple accounts from EPM/Project Server

Sometimes it required to deactivated multiple accounts in EPM. So here is the script which can help you to deactivate multiple accounts. Account deactivation takes so much time so it is recommended to do this activity non business hour.

<# Below is CSV sample which I am using for this code. First Column is headers. #>;
<#
ResourcesEmail
kuldeep.verma@xyz.com
rajdeep.sardesai@xyz.com
Jaydeep.prajapat@xyz.com
#>

#Download and install Microsoft Client Component from https://www.microsoft.com/en-in/download/details.aspx?id=42038
#Microsoft SharePoint Client Component is mandatory to move further.
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.ProjectServer.Client.dll"

#replace CSV path
$path = "<Path of input CSV file>"
$siteURL = "<Replace With PWA URL>"  
$loginname = "<User Account>"  
$pwd = "<Password>"  
$securePassword = ConvertTo-SecureString $pwd -AsPlainText -Force
$creds = New-Object System.Management.Automation.PsCredential $loginname, $securePassword
$projContext = New-Object Microsoft.ProjectServer.Client.ProjectContext($siteURL)
$projContext.Credentials = $creds
$resources = $projContext.EnterpriseResources
$projContext.Load($resources)
$projContext.ExecuteQuery()
Import-Csv $path | ForEach-Object {
    $UserEmail=$_.ResourcesEmail
    <#Check user is available or not in resource center#>
    $resourcesToDeactivate = $resources | ? { $_.Email -eq $UserEmail -and $_.IsActive -eq $true }
    if($null -ne $resourcesToDeactivate)
    {
        $Name =$resourcesToDeactivate.Name
        Write-Host "$Name started to deactivating" -foreground yellow
        $resourcesToDeactivate.IsActive = $false
        $projContext.EnterpriseResources.Update()
        $projContext.ExecuteQuery()
        Write-Host "$Name account has been deactivated" -foreground green
    }
}
Write-Host "All users has been deactivated successfully" -foreground blue

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s