Create Search Service Application in SharePoint 2016 Multi-Server Farm using PowerShell

As its name implies, the Search Service Application in SharePoint provides search functionality. We must use PowerShell scripts to configure the search service application from SharePoint 2013 onwards. Although we can create a new search service application instance, there is no user interface to configure the Search topology from SharePoint Central Admin.

Configuring SharePoint 2016 Search Service Application for Multi-Server Farm:

While my earlier article addresses How to create a search service application in SharePoint 2013 – Single Server farm, for Standalone SharePoint 2013 or SharePoint 2016 installations, In a production environment, we may have to distribute and load balance search components to different servers in a multi-server farm.

Here is the proposed Farm search topology at a high level:

create sharepoint 2016 search service application with Powershell

I have the below servers in my SharePoint 2016 environment using Shared MinRoles:

  • Web Front End + Distributed Cache Servers – 2
  • Application + Search Servers – 2

Since we’ll be using multiple servers in a SharePoint farm, we can add Redundancy, Performance, and Reliability to the search application by splitting the six search components (Admin, Crawl, Content Processing, Indexing, Analytics, and Query Processing) among them. Also, we will configure two index partitions to ensure both servers have a replica of the search index.

PowerShell to Configure Search Service Application in SharePoint 2016:

Modifications to the search topology require a decent understanding of SharePoint search topology and PowerShell. Here is the PowerShell script to create a search service application in a multiserver SharePoint 2016 environment. You can add or modify the topology components according to your environment. Let’s take an example. In my case, we have two SharePoint Search App Servers to host all search components. Creating a search service application for SharePoint 2016 involves these steps at a high level:

  1. Creating a managed account and application pool for search service application.
  2. Creating SharePoint Search Service Application & Proxy
  3. Starting Search service instance on all servers participating in search topology
  4. Creating a Search Topology and adding Search components, and then Activating the search topology

Create Search Service Application using PowerShell:

Here is the PowerShell script to create SharePoint 2016 search service application.

Copy-Paste this script to PowerShell ISE or any other PowerShell editor (PowerGUI?), set the configuration parameters according to your environment and run one step at a time!

Create folders for Index Location & Replica Location (as in variables: $PrimaryIndexLocation and $ReplicaIndexLocation) on all servers which are hosting search topology index components prior to running the main script!

#Set the primary and replica index locations
$PrimaryIndexLocation = "D:\SearchIndex"
$ReplicaIndexLocation = "E:\SearchIndexReplica"

#Prepare Index Locations
Remove-Item -Recurse -Force -LiteralPath $PrimaryIndexLocation -ErrorAction SilentlyContinue
MKDIR -Path $PrimaryIndexLocation -Force
Remove-Item -Recurse -Force -LiteralPath $ReplicaIndexLocation -ErrorAction SilentlyContinue
MKDIR -Path $ReplicaIndexLocation -Force

PowerShell Script to Configure Search Service Application in SharePoint 2016:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Region Config_Parameters
#Settings for Search Service Application
$SearchServiceApplicationName = "Search Service Application"
$SearchServiceApplicationProxyName = "Search Service Application Proxy"
$SearchAppPoolAccount = "Crescent\SP16_Services"
$SearchDatabaseServer = "SP16_SQL"
$SearchServiceApplicationDatabase = "Crescent_Search_ServiceApp"
$SearchAppPoolName = "Service Application App Pool"

#Farm Server topology: App with Search role: 2
$SearchAppServer1 = "SP16-APPSrv01"
$SearchAppServer2 = "SP16-APPSrv02"

#Set the primary and replica index locations
$PrimaryIndexLocation = "D:\SearchIndex"
$ReplicaIndexLocation = "E:\SearchIndexReplica"
#EndRegion


#*** Step 1: Create Managed Account, Service Application Pool for Search Service Application **** 
#Check if Managed account is registered already
Write-Host -ForegroundColor Yellow "Checking if the Managed Accounts already exists..."
$SearchAppPoolAccount = Get-SPManagedAccount -Identity $SearchAppPoolAccount -ErrorAction SilentlyContinue
If ($SearchAppPoolAccount -eq $null)
{
    Write-Host "Please Enter the password for the Service Account..."
    $AppPoolCredentials = Get-Credential $SearchAppPoolAccount
    $SearchAppPoolAccount = New-SPManagedAccount -Credential $AppPoolCredentials
    write-host "Managed Account has been Created!" -ForegroundColor Green
}

#Try to Get the existing Application Pool
Write-Host -ForegroundColor Yellow "Checking if the App Pool already exists..."
$SearchServiceAppPool = Get-SPServiceApplicationPool -Identity $SearchAppPoolName -ErrorAction SilentlyContinue
#If Application pool Doesn't exists, Create it
if ($SearchServiceAppPool -eq $Null)
{ 
    $SearchServiceAppPool = New-SPServiceApplicationPool -Name $SearchAppPoolName -Account $SearchAppPoolAccount 
    write-host "Created New Service Application Pool!" -ForegroundColor Green
}

#*** Step 2: Start Search Service Instances on required servers ***
Write-host "Starting Service Instances..." -ForegroundColor Green
#Search App Server 1
$SearchAppInstance1 = Get-SPEnterpriseSearchServiceInstance $SearchAppServer1
if(-not($SearchAppInstance1.Status -eq "Online"))
{
    Write-Host -ForegroundColor Yellow "Starting Search Service instance on $SearchAppServer1..." -NoNewline
    $SearchAppInstance1 | Start-SPEnterpriseSearchServiceInstance
    while ($SearchAppInstance1.Status -ne "Online")
    {
        Write-Host -ForegroundColor Green "." -NoNewline
        Start-Sleep -Seconds 5
        $SearchAppInstance1 = Get-SPEnterpriseSearchServiceInstance $SearchAppServer1
    }
    Write-Host -ForegroundColor Green "Started Search Service instance on $SearchAppServer1"
}
Start-SPEnterpriseSearchQueryAndSiteSettingsServiceInstance $SearchAppServer1 -ErrorAction SilentlyContinue

#Search App Server 2
$SearchAppInstance2 = Get-SPEnterpriseSearchServiceInstance $SearchAppServer2
if(-not($SearchAppInstance2.Status -eq "Online"))
{
    Write-Host -ForegroundColor Yellow "Starting Search Service instance on $SearchAppServer2..." -NoNewline
    $SearchAppInstance2 | Start-SPEnterpriseSearchServiceInstance
    while ($SearchAppInstance2.Status -ne "Online")
    {
        Write-Host -ForegroundColor Green "." -NoNewline
        Start-Sleep -Seconds 5
        $SearchAppInstance2 = Get-SPEnterpriseSearchServiceInstance $SearchAppServer2
    }
    Write-Host -ForegroundColor Green "Started Search Service instance on $SearchAppServer2"
}
Start-SPEnterpriseSearchQueryAndSiteSettingsServiceInstance $SearchAppServer2 -ErrorAction SilentlyContinue

#*** Step 3: Create Search Service Application and proxy**** 
Write-host "Creating Search Service Application..." -ForegroundColor Yellow
# Get the Search Service Application
$SearchServiceApplication = Get-SPEnterpriseSearchServiceApplication -Identity $SearchServiceApplicationName -ErrorAction SilentlyContinue
# Create the Search Service Application, If it doesn't exist! 
if(!$SearchServiceApplication)
{
    $SearchServiceApplication = New-SPEnterpriseSearchServiceApplication -Name $SearchServiceApplicationName -ApplicationPool $SearchServiceAppPool -DatabaseServer $SearchDatabaseServer -DatabaseName $SearchServiceApplicationDatabase
    write-host "Created New Search Service Application" -ForegroundColor Green
}

#Get the Search Service Application Proxy
$SearchServiceAppProxy = Get-SPEnterpriseSearchServiceApplicationProxy -Identity $SearchServiceApplicationProxyName -ErrorAction SilentlyContinue
# Create the Proxy If it doesn't exist! 
if(!$SearchServiceAppProxy)
{
    $SearchServiceAppProxy = New-SPEnterpriseSearchServiceApplicationProxy -Name $SearchServiceApplicationProxyName -SearchApplication $SearchServiceApplication 
    write-host "Created New Search Service Application Proxy" -ForegroundColor Green
}

#*** Step 4: Create New Search Topology and add components to it
Write-Host -ForegroundColor Yellow "Creating Search Service Topology..."
# Create New Search Topology 
$SearchTopology =  New-SPEnterpriseSearchTopology -SearchApplication $SearchServiceApplication

#Admin Component: App 01 and 02
New-SPEnterpriseSearchAdminComponent -SearchTopology $SearchTopology -SearchServiceInstance $SearchAppInstance1
New-SPEnterpriseSearchAdminComponent -SearchTopology $SearchTopology -SearchServiceInstance $SearchAppInstance2

#Content Processing: App 01 and 02
New-SPEnterpriseSearchContentProcessingComponent -SearchTopology $SearchTopology -SearchServiceInstance $SearchAppInstance1
New-SPEnterpriseSearchContentProcessingComponent -SearchTopology $SearchTopology -SearchServiceInstance $SearchAppInstance2

#Analytics processing: App01 and 02
New-SPEnterpriseSearchAnalyticsProcessingComponent -SearchTopology $SearchTopology -SearchServiceInstance $SearchAppInstance1
New-SPEnterpriseSearchAnalyticsProcessingComponent -SearchTopology $SearchTopology -SearchServiceInstance $SearchAppInstance2

#Crawl components: App 01 and 02
New-SPEnterpriseSearchCrawlComponent -SearchTopology $SearchTopology -SearchServiceInstance $SearchAppInstance1
New-SPEnterpriseSearchCrawlComponent -SearchTopology $SearchTopology -SearchServiceInstance $SearchAppInstance2

#Query processing: App 01 and 02
New-SPEnterpriseSearchQueryProcessingComponent -SearchTopology $SearchTopology -SearchServiceInstance $SearchAppInstance1
New-SPEnterpriseSearchQueryProcessingComponent -SearchTopology $SearchTopology -SearchServiceInstance $SearchAppInstance2

#Create Index Components: App 01 and 02
#Two index partitions and replicas for each partition
New-SPEnterpriseSearchIndexComponent -SearchTopology $SearchTopology -SearchServiceInstance $SearchAppInstance1 -RootDirectory $PrimaryIndexLocation -IndexPartition 0
New-SPEnterpriseSearchIndexComponent -SearchTopology $SearchTopology -SearchServiceInstance $SearchAppInstance2 -RootDirectory $ReplicaIndexLocation -IndexPartition 0
New-SPEnterpriseSearchIndexComponent -SearchTopology $SearchTopology -SearchServiceInstance $SearchAppInstance2 -RootDirectory $PrimaryIndexLocation -IndexPartition 1
New-SPEnterpriseSearchIndexComponent -SearchTopology $SearchTopology -SearchServiceInstance $SearchAppInstance1 -RootDirectory $ReplicaIndexLocation -IndexPartition 1

#Activate the Topology for Search Service
$SearchTopology.Activate() # Or Use: Set-SPEnterpriseSearchTopology -Identity $SearchTopology

#Remove all inactive topologies
$InactiveTopologies = Get-SPEnterpriseSearchTopology -SearchApplication $SearchServiceApplication | Where {$_.State -ne "Active"}
$InactiveTopologies | Remove-SPEnterpriseSearchTopology -Confirm:$false

Write-Host -ForegroundColor Green "Search Service Application has been created. Please start a Full Crawl!"

Once everything is set up, You can see the Search topology visually from the SharePoint Central Administration site.

sharepoint 2016 configure search service application powershell

Scale-out SharePoint 2016 Search to Multiple Servers:

The above script is for a SharePoint farm with two “Application with Search” Servers. What if your SharePoint farm has more than two app servers, say Four?

Best Practice: Assign Query and Index components together in the same server(s) and place remaining components to other Search App servers.
create sharepoint 2016 search service application

Also, in the script, do below changes: Farm Topology Section, include two more servers: Say, App03 and App04

#Farm Server topology: App+Search: 4
$SearchAppServer1 = "Cre-SP16App01"
$SearchAppServer2 = "Cre-SP16App02"
$SearchAppServer3 = "Cre-SP16App03"
$SearchAppServer4 = "Cre-SP16App04"

Start Service instances on app servers 3 & 4 :

#Search App Server 3
$SearchAppInstance3 = Get-SPEnterpriseSearchServiceInstance $SearchAppServer3
if(-not($SearchAppInstance3.Status -eq "Online"))
{
    Write-Host -ForegroundColor Yellow "Starting Search Service instance on $SearchAppServer3..." -NoNewline
    $SearchAppInstance3 | Start-SPEnterpriseSearchServiceInstance
    while ($SearchAppInstance3.Status -ne "Online")
    {
        Write-Host -ForegroundColor Green "." -NoNewline
        Start-Sleep -Seconds 5
        $SearchAppInstance3 = Get-SPEnterpriseSearchServiceInstance $SearchAppServer3
    }
    Write-Host -ForegroundColor Green "Started Search Service instance on $SearchAppServer3"
}
Start-SPEnterpriseSearchQueryAndSiteSettingsServiceInstance $SearchAppServer3 -ErrorAction SilentlyContinue
#Search App Server 4
$SearchAppInstance4 = Get-SPEnterpriseSearchServiceInstance $SearchAppServer4
if(-not($SearchAppInstance4.Status -eq "Online"))
{
    Write-Host -ForegroundColor Yellow "Starting Search Service instance on $SearchAppServer4..." -NoNewline
    $SearchAppInstance4 | Start-SPEnterpriseSearchServiceInstance
    while ($SearchAppInstance4.Status -ne "Online")
    {
        Write-Host -ForegroundColor Green "." -NoNewline
        Start-Sleep -Seconds 5
        $SearchAppInstance4 = Get-SPEnterpriseSearchServiceInstance $SearchAppServer4
    }
    Write-Host -ForegroundColor Green "Started Search Service instance on $SearchAppServer4"
}
Start-SPEnterpriseSearchQueryAndSiteSettingsServiceInstance $SearchAppServer4 -ErrorAction SilentlyContinue 

On creating Search topology: Assign Query and Index Roles to App Servers 3 and 4

#Query processing: App 03 and 04
New-SPEnterpriseSearchQueryProcessingComponent -SearchTopology $SearchTopology -SearchServiceInstance $SearchAppInstance3
New-SPEnterpriseSearchQueryProcessingComponent -SearchTopology $SearchTopology -SearchServiceInstance $SearchAppInstance4

#Create Index Components: App 03 and 04
#Two index partitions and replicas for each partition
New-SPEnterpriseSearchIndexComponent -SearchTopology $SearchTopology -SearchServiceInstance $SearchAppInstance3 -RootDirectory $PrimaryIndexLocation -IndexPartition 0
New-SPEnterpriseSearchIndexComponent -SearchTopology $SearchTopology -SearchServiceInstance $SearchAppInstance4 -RootDirectory $ReplicaIndexLocation -IndexPartition 0
New-SPEnterpriseSearchIndexComponent -SearchTopology $SearchTopology -SearchServiceInstance $SearchAppInstance4 -RootDirectory $PrimaryIndexLocation -IndexPartition 1
New-SPEnterpriseSearchIndexComponent -SearchTopology $SearchTopology -SearchServiceInstance $SearchAppInstance3 -RootDirectory $ReplicaIndexLocation -IndexPartition 1

Next Steps:

  • Create Content Sources and schedules 
  • Create a Search center site 
  • Configure Crawl account & search center URL for the Search service application.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

10 thoughts on “Create Search Service Application in SharePoint 2016 Multi-Server Farm using PowerShell

  • Just and FYI when running this script in a mini role environment, the only way I got it to work was to ensure Central Admin was running on the search server and AAM pointing to search server and launching it form the search server.

    Reply
  • OK I checked which components it thought it had created and it came back with 0 so ran the entire script again but still getting search-service-is-not-able-to-connect-to-the-machine-that-hosts-the-administration-component error at end and the search never finishes getting set up

    Reply
  • Question – it gave me an error at various points – how I can re-run without it mucking up all the bits it created successfully?

    Reply
  • Thanks for sharing this.

    Reply
  • I have a below topology in my sp 2016 farm.. 1 application withwsearch n 2. WFE I have configured the search service in app server n all web application hosted in wfe server .. crawl completed without any error but when I search anything from the site it just rotate the search box then get the time out error … What should I check?? Any help on this.

    Reply
  • Oh this is really good. Thanks for publishing this helpful article about how to configure Search Service Application in SharePoint 2016 using PowerShell scripts, great one. Also, thanks much for sharing the step-by-steps along with screenshots as this is very much useful. I like SharePoint 2016 as this version has released with so many exciting features to give a better user experience.

    Reply
  • Great work Salaudeen. Thx for share.

    Reply
  • Amazing ! Thank you for sharing.

    Reply
  • I’m a Newbie, I am constantly exploring online and this one assisted me. Thank you!

    Reply
  • Thank you. God bless you!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *