Web Applications
Each site collection in SharePoint is associated with a single web application represented by the SPWebApplication object. The SPWebApplication
object contains many properties that an administrator might want to
look at, such as those associated with the Recycle Bin, list
throttling, and status of the web application. Using the SharePoint
PowerShell commands, you can list all web applications, including the
Central Administration web application; create new web applications;
remove web applications; and modify web application properties.
To begin, suppose you want to see all the web applications on the farm. As shown previously, by default, the Get-SPWebApplication
command returns all the web applications on the farm except for the
Central Administration web application. To get all web applications on
a farm, including the Central Administration web application, use the Get-SPWebApplication command with the IncludeCentralAdministration switch parameter.
Getting all the web applications might not be
exactly what you need. Sometimes you need a single web application. To
do that, you use the –Identity parameter. This parameter is smart enough to accept the name, URL, or ID of the web application. You rarely see the –Identity parameter actually named. As shown earlier with the Format-Table command, you can omit the name of the parameter, and name only the URL or ID as shown in Figure 4.
The SPWebApplication has a lot of properties and methods. To see the properties of this specific web application, you can pipe it to Format-List, as shown in Figure 5.
As you can see, the web application object has a
lot of properties. To be honest, all the SharePoint objects have a lot
of properties. How are you supposed to discover all these properties
and keep track of them? Fortunately, there is a command for that. Get-Member
will return the members of the object passed into it via the pipeline.
To determine what properties and methods are available on the SPWebApplication object, use the following command (the output is shown in Figure 6):
Get-SPWebApplication http://portal.contoso.com | Get-Member
If piping the output through Get-Member returns more information than one screen can handle, you can additionally pipe it to the Out-Host -paging cmdlet:
Get-SPWebApplication http://portal.contoso.com | Get-Member | out-host -paging
This command returns one page of output at a
time. Press the spacebar to see the next screen. You can also scroll
one line at a time by pressing the Enter key.
To access a specific property on the SPWebApplication object, use the dot notation described earlier. To access the ID of the http://portal.contoso.com web application, use the following command:
(Get-SPWebApplication http://portal.contoso.com).Id
In addition to being able to access the web
applications, you can also use PowerShell to create or remove a web
application. It’s clear why you might want to remove a web application,
but why would you want to create a new one using PowerShell? Using
PowerShell to create new web applications can be useful for repeatedly
building out development or demo environments. The following New-SPWebApplication
cmdlet creates a new web application, which contains many parameters —
some required and some optional. To see all the parameters, use Get-Help New-SPWebApplication.
New-SPWebApplication –Name "Portal" –Port 80 –HostHeader portal.contoso.com
–Url
http://portal.contoso.com –ApplicationPool "Default SharePoint Web App Pool"
–ApplicationPoolAccount (Get -SPManagedAccount contoso\SP_webeapps)
NOTE When
running the preceding code, you may get an error indicating that you
need “machine privileges.” This happens if your SharePoint 2013
Management Shell was not started with the Run as Administrator option.
Without elevated permissions, SharePoint can’t always access what it
needs. If you get an error like this, ensure that your Management Shell
window has “Administrator:” at the beginning of the title bar.
After creating your new web application, you can use the Get-SPWebApplication command to verify that it was created properly. However, you may have noticed that the New-SPWebApplication
cmdlet output the web application’s information upon completion. You
could capture that object directly in a variable or pass the object
onto another command via the pipeline. Notice that you are calling the Get-SPManagedAccount command to retrieve an SPManagedAccount object, which is required for the AppPool account. You can see what managed accounts you have by using the Get-SPManagedAccount command with no parameter, as shown in Figure 7.
Of course, creating a web application through PowerShell wouldn’t be very useful if you couldn’t similarly remove it. The Remove-SPWebApplication
command follows the same verb-noun convention discussed previously, and
as you might expect it is used to remove the specified web application
from the farm. This command requires you to select a specific web
application, which prevents the deletion of multiple applications at
once. Figure 8 shows how to remove the web application you just created.
Notice how PowerShell prompts you before
permanently destroying your data. The cmdlet offers a way to “override”
this prompt, but we leave that as a future exercise once you are
comfortable enough with PowerShell that you won’t accidentally destroy
production data.