Creating Your SharePoint 2013 Farm with PowerShell
The following script completely replaces using the
Configuration Wizard to build your SharePoint 2013 farm:
# Add the SharePoint Snapin, in case PowerShell wasn't started with the Management
Shell
Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
# Verify that PowerShell is running as an Admin
if ( -not ([Security.Principal.WindowsPrincipal] [Security.Principal.
WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]
"Administrator"))
{
Write-Output "This PowerShell prompt is not elevated"
write-output "Please start PowerShell with the Admin token and try again"
return
}
# Everything looks good. Let's build us a SharePoint farm
$domain = (Get-ChildItem env:userdomain).value
$tempfarmaccount = $domain.ToString() + '\sp_farm'
$tempsqlserver = (Get-ChildItem env:computername).value
$farmaccountname = Read-Host -Prompt "Enter Farm Account Name. Press Enter for
$tempfarmaccount"
If ($farmaccountname -eq "") {$farmaccountname = $tempfarmaccount}
$farmaccountpassword = Read-Host -Prompt "Enter Farm Account Password. Press Enter
for pass@word1"
If ($farmaccountpassword -eq "") {$farmaccountpassword = 'pass@word1'}
$farmpassphrase = Read-Host -Prompt "Enter Farm Passphrase. Press Enter for
pass@word1"
If ($farmpassphrase -eq "") {$farmpassphrase = 'pass@word1'}
$sqlserver = Read-Host -Prompt "Enter SQL Instance name. Press Enter for
$tempsqlserver"
If ($sqlserver -eq "") {$sqlserver = $tempsqlserver}
$password = ConvertTo-SecureString $farmaccountpassword -AsPlainText -Force
$farmaccount = New-Object system.management.automation.pscredential
$farmaccountname, $password
Write-Host "Using that information to build your SharePoint Farm"
New-SPConfigurationDatabase -DatabaseName SharePoint_Config -DatabaseServer
$sqlserver -AdministrationContentDatabaseName SharePoint_Admin_Content
-Passphrase (convertto-securestring $farmpassphrase -AsPlainText -Force)
-FarmCredentials $farmaccount
Write-Host "Config database built, now configuring local machine."
Install-SPHelpCollection -All
Initialize-SPResourceSecurity
Install-SPService
Install-SPFeature -AllExistingFeatures
Write-host "Creating Central Admin on port 10260"
New-SPCentralAdministration -Port 10260 -WindowsAuthProvider "NTLM"
Install-SPApplicationContent
As indicated by the comments, this script will
work with SharePoint 2010 or SharePoint 2013. It employs not only
several SharePoint cmdlets we have covered (those for which the noun
starts with “SP”), but also many new PowerShell techniques such as
asking for input, error control, and flow control. Once the script has
completed successfully, your farm will be created, including Central
Administration.
Creating Managed Accounts and Service Application Pools
When you’re configuring your farm, one
of the first things you need to do is create the managed accounts and
service app application pools your farm will use. The following
PowerShell lines will do that:
# Create Managed Accounts
$password = ConvertTo-SecureString 'pass@word1' -AsPlainText -Force
# create sp_webapps
$account = New-Object system.management.automation.pscredential
'contoso\sp_webapps', $password
New-SPManagedAccount $account
# create sp_serviceapps
$account = New-Object system.management.automation.pscredential
'contoso\sp_serviceapps', $password
New-SPManagedAccount $account
# Create the Service App Pool
New-SPServiceApplicationPool -Name "Default SharePoint Service App Pool"
-Account contoso\sp_serviceapps
$apppool = Get-SPServiceApplicationPool "Default SharePoint Service App Pool"
Of course, you need to ensure that the
usernames and passwords match those in your actual farm. The chances
are pretty slim that your domain is Contoso.