3. Installing roles and features using Windows PowerShell
You can also install roles and features on servers running
Windows Server 2012 by using Windows PowerShell commands. This
approach can be useful for administrators who work in midsized to
large environments that have many servers deployed—for example, in a
datacenter. You can also use PowerShell to install roles and features
on offline virtual disks.
The following Server Manager cmdlets can be used for managing
roles and features using PowerShell:
-
Get-WindowsFeature Retrieves
information about Windows Server roles, role services, and
features that are available
-
Install-WindowsFeature
Installs one or more Windows Server roles, role services, or
features
-
Uninstall-WindowsFeature
Uninstalls and removes specified Windows Server roles, role
services, and features
Important
Running Server Manager cmdlets
Server Manager cmdlets must be run elevated.
Retrieving a list of installed roles and features
You can use the Get-WindowsFeature cmdlet to retrieve
information about roles, role services, and features available on a
remote server. For example, the following command displays a list of
all available roles and features and their current install state on
server SEA-SRV-1:
Get-WindowsFeature -ComputerName SEA-SRV-1
To display a list of all installed roles and features on the
server, pipe the output of the preceding command into the
Where-Object cmdlet and use Where-Object to filter out everything
except roles and features whose InstallState
property is equal to Installed:
Get-WindowsFeature -ComputerName SEA-SRV-1 | Where-Object InstallState -eq Installed
You can narrow your results even further by using the
–name parameter of the Get-WindowsFeature
cmdlet to select only roles and features that begin with “Print”like
this:
Get-WindowsFeature -Name Print* -ComputerName SEA-SRV-1 | Where-Object InstallState -eq
Installed
The output from running this command against server SEA-SRV-1
verifies that the Print Server role service of the Print And
Document Services role is installed:
Display Name Name Install State
------------ ---- -------------
[X] Print and Document Services Print-Services Installed
[X] Print Server Print-Server Installed
Note
Windows PowerShell
3.0 simplified syntax
One of the improvements in version 3.0 of Windows PowerShell
is the simplification of the syntax for the Where-Object cmdlet.
If you are an administrator who already has some familiarity with
using PowerShell for managing Windows servers, you might have
wondered about the syntax of some of the commands in this section.
In particular, you might have wondered why the second example
didn’t look like this:
Get-WindowsFeature - ComputerName SEA-SRV-1 | Where-Object {$_.InstallState -
eq Installed}
The reason is because Windows PowerShell 3.0 lets you
eliminate the script block notation (the curly braces), the
current object placeholder ($_), and the dot property notation.
These improvements make PowerShell code easier to
understand.
Installing roles and features
You can use the Install-WindowsFeature cmdlet to install
roles, role services, and features available on a remote server. You
can also use the alias Add-WindowsFeature to invoke this command.
For example, the following command installs the DHCP Server role on
server SEA-SRV-1:
Install-WindowsFeature -Name DHCP -ComputerName SEA-SRV-1
The output from running the preceding command looks like
this:
Success Restart Needed Exit Code Feature Result
------- -------------- --------- --------------
True No Success {DHCP Server}
To install the DHCP Server role together with the management
tools for this role, add the
–IncludeManagementTools parameter to the
preceding command. If a restart is required for the installation of
a role or feature to install properly, you can force this to happen
by including the –Restart parameter in the
command.
To install all of the Remote Server Administration features on
this server, use this command:
Install-WindowsFeature -Name RSAT -IncludeAllSubFeature -ComputerName SEA-SRV-1
Note
MORE INFO For more examples
of how to use the Install-WindowsFeature cmdlet, type Get-Help Install-WindowsFeature –examples
in the PowerShell console.
Installing roles and features on multiple servers
Although Server Manager can be used to install roles and
features only on a single server at a time, you can use PowerShell
to install roles and features on multiple computers at the same
time. You can do this by using the Invoke-Command cmdlet to run the
Install-WindowsFeature command on multiple computers. For example,
this command installs the XPS Viewer feature on servers SEA-SRV-1
and SEA-SRV-3:
Invoke-Command -ComputerName SEA-SRV-1, SEA-SRV-3 -ScriptBlock {Install-WindowsFeature -
Name XPS-Viewer}
The output from running the preceding command looks like
this:
Success Restart Needed Exit Code Feature Result PSComputerName
------- -------------- --------- -------------- --------------
True No Success {XPS Viewer} SEA-SRV-1
True No Success {XPS Viewer} SEA-SRV-3
Note
Installing and removing roles or features from multiple
computers
You can use the Invoke-Command with the Server Manager
cmdlets to install or remove roles or features on only up to 32
computers at a time. If you specify more than 32 computers, the
additional computers will be queued. You can use the
ThrottleLimit parameter to override this
behavior.
Installing roles and features for which the payload has been
removed
If the binaries for the role or feature you want to install
have been removed from the server, you can use the
–Source parameter to specify the location of
the binaries needed to install the role or feature. If you don’t
specify this parameter, the necessary binaries will be downloaded
from Windows Update unless this behavior has been disabled using
Group Policy. Note that downloading role or feature binaries from
Windows Update can take some time.
Quick check
Quick check answer