Introduction

In this article, we will show How to Bulk register Managed Account In SharePoint Using PowerShell?



What's the Managed Account in SharePoint?

A Managed Account is effectively an Active Directory user account whose credentials are managed by and contained within SharePoint. Read more at Managed Accounts in SharePoint.


Add Managed Account In SharePoint via PowerShell

You can easily register a managed account via New-SPManagedAccount cmdlet as below

$pass = convertto-securestring $Password -asplaintext -force
$cred = new-object management.automation.pscredential $Account ,$pass
$res = New-SPManagedAccount -Credential $cred

But what if you want to register a large number of accounts, it will be a boring process to add each account one by one!
so we wrote the below script that helps to bulk register Managed account in SharePoint.



Bulk Register Managed Account In SharePoint via PowerShell

You can bulk register managed account by looping for each item in CSV file as below.

PowerShell Script


#######################################################
#Add Add-PSSnapin Microsoft.SharePoint.PowerShell
Set-ExecutionPolicy "Unrestricted"
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction Stop
#######################################################
#Add service account to managed account
function Add-ManagedAccount()
  {
  Try
   {
    #Get Accounts from CSV
    Import-Csv F:\ManagedAccounts.csv | ForEach-Object {
    $ServiceAccount= $_."Service Account"
    $AccountPassword= $_.Password
    Write-Host "Adding the service Account" $ServiceAccount "to Managed Account" -ForegroundColor Green
    $srvacount = Get-SPManagedAccount | ?  {$_.UserName -eq $ServiceAccount}
    if ($srvacount -eq $null)
        {
        $pass = convertto-securestring $AccountPassword -asplaintext -force
        $cred = new-object management.automation.pscredential $ServiceAccount ,$pass
        $res = New-SPManagedAccount -Credential $cred
         if ($res -ne $null)
            {
                Write-Host "The" $ServiceAccount "has been added successfully to Managed Account" -ForegroundColor Cyan
            }
        }
    else
        {
         Write-Host "The" $ServiceAccount "is already added to Managed Account" -ForegroundColor Yellow
        }
      }
    }
  Catch
    {
    Write-Host $_.Exception.Message -ForegroundColor Red
    break
    }
  }
 
#Add bulk accounts to managed accounts using PowerShell
Add-ManagedAccount

The CSV file has two columns: 
  • Service Account.
  • Password.

Note: You can download the full script and the CSV template file from Technet Gallerty.

How to use the script?


Before you run this script, you should make sure that the current user has sufficient privilege to run PowerShell Script in SharePoint, Read more at Add-SPShellAdmin.
  • Download the CSV template file and the script from Technet Gallerty.
  • Fill the CSV file with your managed accounts list (Domain\Username,Password)
  • Run ISE as Administrator.
  • Open the downloaded script file.
  • Update the CSV file path, then Click Run.


Script Output


You have two expected output result as the following:
  1. In case the account is not added to the Managed Account, you will get 'The User Account has been added successfully to Managed Account'.
  2. In case the account is already added before to the Managed Account, you will get 'The User Account is already added to Managed Account'.

Applies To

  • SharePoint 2016.
  • SharePoint 2013.
  • SharePoint 2010.

Download

Download the script file "PowerShell Script: Bulk Register Managed Account In SharePoint" from TechNet Gallery.

Conclusion

In this article, we have explained how to bulk register Managed Account In SharePoint Using PowerShell?

See Also