Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 Windows OS Hub / PowerShell / Managing Windows Server Roles & Features with PowerShell

May 10, 2023 PowerShellWindows Server 2012 R2Windows Server 2016Windows Server 2019

Managing Windows Server Roles & Features with PowerShell

In Windows Server 2012R2/2016/2019, you can use the graphical Server Manager console to install and remove server roles and features. However, in most cases you can do the same from the PowerShell console much faster. In this article we’ll consider how to manage roles and features in the modern Windows Server versions with PowerShell.

Contents:
  • List all Installed Windows Server Roles & Feature via PowerShell
  • How to Install Windows Server Roles & Features using PowerShell?
  • How to Deploy Roles on Multiple Remote Windows Servers?
  • How to Uninstall a Role or Feature on Windows Server with PowerShell?

List all Installed Windows Server Roles & Feature via PowerShell

User the Get-WindowsFeature cmdlet to display the list of all available Windows Server roles and features. If you run it without parameters, you will see the information about all Windows Server components.

The name of a component (Display Name), its system name (Name) and state (Install State: Installed, Available or Removed) are displayed. The list of roles and features looks like a tree with the nested roles similar to the one you see when you install the roles in the Server Manager GUI. To install and remove any roles or features using PowerShell, you must know their system names listed in the Name column.

Get-WindowsFeature get all available roles and features on windows server via powershell

Hint. If a role or a feature is Removed, it means that its installation files are removed from the system component sote (to reduce the size of WinSxS folder) and you won’t be able to install the role without direct Internet access or Windows Server installation ISO (see the example with the .Net 3.5 installation).

You can remove roles or components from your image online like this:

Uninstall-WindowsFeature –Name DHCP –Remove

To install a removed DHCP role, use this cmdlet:

Install-WindowsFeature DHCP (you will need the direct Internet access)

Or you can restore the component binary files from your Windows Server ISO image:

Install-WindowsFeature DHCP -Source E:\sources\sxs

You can list the installed server features:

Get-WindowsFeature | Where-Object {$_. installstate -eq "installed"} | ft Name,Installstate

Based upon the screenshot below, this server is used as a file server (FileAndStorage-Services, Storage-Services roles installed). Most of other components are used to manage or monitor the server.

Get-WindowsFeature installed

If you do not know the role name exactly, you can use wildcards. For example, to check what web components of the IIS role are installed, run this command (the syntax is a bit shortened):

Get-WindowsFeature -Name web-* | Where installed

Get-WindowsFeature Name like web

You can get the list of installed components on a remote Windows Server:

Get-WindowsFeature -ComputerName ny-spool1 | Where installed | ft Name,Installstate

Judging by the installed Print-Services and Print-Server roles, this server is used as a print server.

Get-WindowsFeature installed on remote Windows Server

You can use the Get-WindowsFeature cmdlet to find servers in your domain, on which the specific role is installed. You can search your servers in a particular Active Directory OU using the Get-ADComputer cmdlet from the PowerShell ActiveDirectory module or by the provided list of servers ($servers = ('server1', 'server2')).

For example, you want to find all file servers with the FileAndStorage-Services role in the specified AD organizational unit (I am using the Visual Studio Code as a PoweShell editor). Use the following script:

import-module activedirectory
$Servers=get-adcomputer -properties * -Filter {Operatingsystem -notlike "*2008 R2*" -and enabled -eq "true" -and Operatingsystem -like "*Windows Server*"} -SearchBase ‘OU=Servers,OU=UK,DC=woshub,DC=com’ |select name
Foreach ($server in $Servers)
{
Get-WindowsFeature -name FileAndStorage-Services -ComputerName $server.Name | Where installed | ft $server.name, Name, Installstate
}

In the output, you will get the list of servers, on which the specific role is installed.
find windows server in ad domain with specific role installed

How to Install Windows Server Roles & Features using PowerShell?

In order to install roles and features on Windows Server, the Install-WindowsFeature cmdlet is used.

To install the DNS server role and the management tools (including the Powershell DNSServer module) on the current server, run this command:

Install-WindowsFeature DNS -IncludeManagementTools

By default, the cmdlet installs all dependent roles and features. To display the list of dependencies prior to the installation, use the option WhatIf:
Install-WindowsFeature -Name UpdateServices -WhatIf

For example, to install the WSUS role, you will have to install some IIS components as well.

What if: Continue with installation?
What if: Performing installation for "[Windows Server Update Services] Windows Server Update
What if: Performing installation for "[Windows Server Update Services] WID Database".
What if: Performing installation for "[Windows Server Update Services] WSUS Services".
What if: Performing installation for "[Web Server (IIS)] Windows Authentication".
What if: Performing installation for "[Web Server (IIS)] Dynamic Content Compression".
What if: Performing installation for "[Web Server (IIS)] Performance".
What if: Performing installation for "[Web Server (IIS)] Static Content".
What if: Performing installation for "[Windows Internal Database] Windows Internal Database".
What if: The target server may need to be restarted after the installation completes.

To install the Remote Desktop Session Host role, the RDS licensing role and RDS remote management tools, use the following command:

Install-WindowsFeature -ComputerName lon-rds3 RDS-RD-Server, RDS-Licensing –IncludeAllSubFeature –IncludeManagementTools –Restart

Install-WindowsFeature on multiple servers

If you add the –Restart parameter, your server will be automatically restarted if required.

You can also install a component with the following command. For example, to install the SMTP server role:

Get-WindowsFeature -Name SMTP-Server | Install-WindowsFeature

How to Deploy Roles on Multiple Remote Windows Servers?

There is another interesting option when you deploy typical servers. You can install the features you want on a reference Windows Server and export the list of the installed roles to a CSV file:

Get-WindowsFeature | where{$_.Installed -eq $True} | select name | Export-Csv C:\PS\InstalledRoles.csv -NoTypeInformation –Verbose

Exporting all Windows Features installed to csv file

Then you will be able to use this CSV file to install the same set of roles on other typical servers:

Import-Csv C:\PS\Roles.csv | foreach{ Install-WindowsFeature $_.name }

import csv file with roles and features on windows server

If a role or a feature is already installed, the command will return NoChangeNeeded and continue with the installation of the next role.

Or to install the same role set on multiple remote servers, you can use this command:

$servers = ('ny-rds1', 'ny-rds2',’ny-rds3’,’ny-rds4’)
foreach ($server in $servers) {Install-WindowsFeature RDS-RD-Server -ComputerName $server}

How to Uninstall a Role or Feature on Windows Server with PowerShell?

To remove Windows Server roles or feture, the Remove-WindowsFeature cmdlet is used.

For example, to remove a print server role, run the command:

Remove-WindowsFeature Print-Server -Restart

1 comment
2
Facebook Twitter Google + Pinterest
previous post
Get-ADDomainController: Getting Domain Controllers Info via PowerShell
next post
How to Mirror (RAID1) Boot GPT Hard Drive on Windows 10/ Server 2016?

Related Reading

Zabbix: How to Get Data from PowerShell Scripts

October 27, 2023

Tracking Printer Usage with Windows Event Viewer Logs

October 19, 2023

PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

October 15, 2023

How to Query and Change Teams User Presence...

October 8, 2023

How to Use Ansible to Manage Windows Machines

September 25, 2023

1 comment

Adam February 25, 2021 - 2:36 pm

I appreciate the effort in putting this together. I learned quite a bit here.

However, the section I was particularly interested in — “How to Deploy Roles on Multiple Remote Windows Servers” — didn’t work out very well for me. I’m not sure why yet, but a number of roles/features that were not on the source server ended up getting installed on the destination server. So, I had to go back and uninstall those “extra” roles.

That said, this is a good place to start learning on the topic. I just wouldn’t use it to install roles/features on a production server without first testing things out on a VM or in a lab.

Reply

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Zabbix: How to Get Data from PowerShell Scripts

    October 27, 2023
  • Tracking Printer Usage with Windows Event Viewer Logs

    October 19, 2023
  • PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

    October 15, 2023
  • Reset Root Password in VMware ESXi

    October 12, 2023
  • How to Query and Change Teams User Presence Status with PowerShell

    October 8, 2023
  • How to Increase Size of Disk Partition in Ubuntu

    October 5, 2023
  • How to Use Ansible to Manage Windows Machines

    September 25, 2023
  • Installing Language Pack in Windows 10/11 with PowerShell

    September 15, 2023
  • Configure Email Forwarding for Mailbox on Exchange Server/Microsoft 365

    September 14, 2023
  • How to View and Change BIOS (UEFI) Settings with PowerShell

    September 13, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Get-ADUser: Find Active Directory User Info with PowerShell
  • How to Hide Installed Programs in Windows 10 and 11
  • PowerShell: Get Folder Sizes on Disk in Windows
  • Managing Printers and Drivers with PowerShell in Windows 10 / Server 2016
  • Deploy PowerShell Active Directory Module without Installing RSAT
  • Managing User Photos in Active Directory Using ThumbnailPhoto Attribute
  • Managing Saved Passwords Using Windows Credential Manager
Footer Logo

@2014 - 2023 - Windows OS Hub. All about operating systems for sysadmins


Back To Top