SharePoint 2013 Search Configuration PowerShell Script

So last month I promised some customers I would post my
search script for 2013 and as life would have it, August came before I got to
it so here it is…. Better late than never. 
While this script is pretty basic as far as provisioning Search for
SharePoint 2013 goes it does address two things I always try to do when I provision
search that I don’t often see.  First, it
is designed to use two different service accounts, one for the administration
application pool and the other for the query application pool.  Second, at the end of the script I put in a
piece that sets the content access account to another account which follows
best practice. 

This particular code is designed for a standard 3 tier farm
that has an Application, Web and Database server.  The idea of the code is to put most of the
Search roles on the Application server. 
The roles going on the Web server are related to the Query Role and


#Setup Variables - Assume 2 Servers (App & Web)

#Assume 3 Accounts (Content Access, Admin, Query)

#### Note - You should setup the Index Location prior to
running these commands and ensure it is an empty folder

#### Note - If you setup diag logging prior to running
script, ensure you give dbo permissions to the account you run this under

write-host 0. Setting up some initial variables.

$AppSvr = "SPApp2013"

$WebSvr = "SPWeb2013"

$CAAcct = "campbushnell\svcSPSearchCA"

$password = "ZAQ!2wsx"

$CAAcctPW = ConvertTo-SecureString -String $password
-AsPlainText -Force

$AdminAcct = "campbushnell\svcSPSearchA"

$QueryAcct = "campbushnell\svcSPSearchQ"

$SSAName = "EnterpriseSearch"

$SSIAppSvr = get-SPServiceInstance -server $AppSvr |
?{$_.TypeName -eq "SharePoint Server Search"}

$SSIWebSvr = get-SPServiceInstance -server $WebSvr |
?{$_.TypeName -eq "SharePoint Server Search"}

$IndexLocation = "C:\SPIndexLocation"

$err = $null



#Start Services for SSI

write-host 1. Start Services search services for Each Server

Start-SPEnterpriseSearchServiceInstance -Identity $SSIAppSvr

Start-SPEnterpriseSearchServiceInstance -Identity $SSIWebSvr



#Create Application Pools

write-host 2. Create a Application Pools

$AdminAppPool = new-SPServiceApplicationPool -name
$SSAName"-AdminAppPool" -account $AdminAcct 

$QueryAppPool = new-SPServiceApplicationPool -name
$SSAName"-QueryAppPool" -account $QueryAcct



#Create Search Service Application

write-host 3. Create the SearchApplication and set it to a

$SearchApp = New-SPEnterpriseSearchServiceApplication -Name
$SSAName -applicationpool $AdminAppPool -databasename



#Create Search Service Application Proxy

write-host 4. Create search service application proxy

$SSAProxy = new-spenterprisesearchserviceapplicationproxy
-name $SSAName"ApplicationProxy" -SearchApplication $SearchApp



#Clone Search Topology (this is empty by default)

write-host 5. Clone Search Topology

$SearchTopology = $SearchApp.ActiveTopology.Clone()



#Provision Search Administration Component

write-host 6. Provision Search Admin Component.

New-SPEnterpriseSearchAdminComponent -SearchServiceInstance
$SSIAppSvr -SearchTopology $SearchTopology



#Provision New Search Content Processing Component

write-host 7. Create New Search Content Processing Component

-SearchServiceInstance $SSIAppSvr -SearchTopology $SearchTopology



#Provision New Search Analytics Processing Component

write-host 8. Create New Search Analytics Processing

-SearchServiceInstance $SSIAppSvr -SearchTopology $SearchTopology



#Provision New Search Crawl Component

write-host 9. Create New Search Crawl Component

New-SPEnterpriseSearchCrawlComponent -SearchServiceInstance
$SSIAppSvr -SearchTopology $SearchTopology



#Provision New Search Index Component

write-host 10. Create New Search Index Component

New-SPEnterpriseSearchIndexComponent -SearchServiceInstance
$SSIWebSvr -SearchTopology $SearchTopology -RootDirectory $IndexLocation



#Provison New Query Processing Component

write-host 11. Create New Query Processing Component

-SearchServiceInstance $SSIWebSvr -SearchTopology $SearchTopology



#Activate Topology Changes

write-host 12. Activate Topology




#Setup default Content Access Account

write-host 13. Set the default content access account

Set-SPEnterpriseSearchServiceApplication –Identity
$SearchApp -ApplicationPool $QueryAppPool -DefaultContentAccessAccountName
$CAAcct -DefaultContentAccessAccountPassword $CAAcctPW


Write-host "Your search application $SSAName is now


One last note with this script, it is designed for smaller
farms and if a large search farm is being created more configuration will be
required to ensure you get the performance you are looking for.