Working with Windows Server roles and features using PowerShell

PowerShell is a very handy tool. PowerShell not only helps in performing actions in bulk but also helps in saving the time it takes to perform the same set of actions using GUI tools. For example, if you need to enable multiple user accounts in Active Directory, most admins prefer to use PowerShell rather than using the Active Directory Users and Computers snap-in. Similarly, PowerShell can be used to manage roles and features on Windows Servers. While you can use Server Manager to connect to a remote Windows Server and then perform the installation and uninstallation of Windows Server roles and features, using Server Manager might take a considerable amount of time. This article explains some of the useful PowerShell commands that you can use to manage roles and features on remote Windows Servers.

Creating a text file that holds Windows Server names

Since we would like to use the PowerShell commands explained in this article on multiple Windows Servers, we will be required to create a text file that holds the server names. The file should contain a fully qualified domain name (FQDN) of each Windows Server name. Once you have created the file that holds the Windows Server FQDN, proceed with the next sections of this article. Let’s assume we have created a file by the name WinServers.TXT and it’s located under C:\Temp on a computer from where we execute below PowerShell commands.

PowerShell commands to use

Windows Server roles and features
Microsoft provides three PowerShell cmdlets to work with both Windows Server roles and features: Install-WindowsFeature, Get-WindowsFeature, and Uninstall-WindowsFeature. These three cmdlets can be used to install, get a list of features and roles installed on the target Windows Servers, and also uninstall features or roles. We will use the Get-WindowsFeature PowerShell cmdlet to prepare a report that lists the roles and features installed for each of our Windows Servers.

Installing and uninstalling Windows Server roles and features

In case you need to install Windows features or roles on a single Windows computer, you can execute this PowerShell command. The command installs the “Web-Server” role on a computer named TestServer.TechGenix.com.


Install-WindowsFeature –Name Web-Server –ComputerName TestServer.TechGenix.com


As you can see, it’s easier to install the role using the above command. In case you wish to install the “Web-Server” role on Windows Servers specified in the C:\Temp\WinServers.TXT file, you can use this tiny PowerShell script.


$WinServer = “C:\Temp\WinServers.TXT”
Foreach ($ThisServer in Get-Content “$WinServer”)
{
Install-WindowsFeature –Name Web-Server –ComputerName $ThisServer
}


Uninstalling Windows Server roles and features is quite simple. You will be required to use Uninstall-WindowsFeature PowerShell cmdlet as shown in the examples below:

To uninstall a Windows feature or role from a single computer, execute this command:


Uninstall-WindowsFeature –Name Web-Server –ComputerName TestServer.TechGenix.com


If you need to provide credentials for uninstallation, simply add –Credential parameter as shown in the command below:


Uninstall-WindowsFeature –Name Web-Server –ComputerName TestServer.TechGenix.com –Credential TechGenix\Admin


Listing Windows Server roles and features and preparing a report

If you need to ensure all production Windows Servers have required roles and features installed and that no extra roles and features are installed, you can use this PowerShell script:


$WinServer = “C:\Temp\WinServers.TXT”
$Report = “C:\Temp\RoleFeaReport.CSV”
$STR = “Server Name, Role/Feature, State”
Add-Content $Report $STR
Foreach ($ThisServer in Get-Content “$WinServer”)
{
$AllRF = Get-WindowsFeature –ComputerName $ThisServer
ForEach ($AllItems in $AllRF)
{
$NameNow = $AllItems.Name
$StatusNow = $AllItems.”Install State”

IF ($StatusNow -eq “Installed”)
{
$STRNew = $ThisServer+”,”+$NameNow+”,Installed”
Add-Content $Report $STRNew
}
}
}


The PowerShell script above queries all Windows Servers mentioned in the C:\Temp\WinServers.TXT, executes Get-WindowsFeature command against the server, retrieves the list of roles and features on the target computer, stores in the $AllRF variable, and then next ForEach loop processes the entries in $AllRF variable and only looks for “Installed” roles/features. If it finds any installed roles and features, it adds the entry with the server name in the C:\Temp\RoleFeaReport.CSV file.

We provided a few examples of using Get-WindowsFeature, Install-WindowsFeature, and Uninstall-WindowsFeature PowerShell cmdlets to manage Windows Server roles and features. We also provided a PowerShell script that you can use to keep a track of roles and features installed on the production Windows Servers. The PowerShell script provides a report in the CSV file.

About The Author

2 thoughts on “Working with Windows Server roles and features using PowerShell”

  1. Matheen Labeeb

    Hi Nirmal,
    Thanks for the script. I tried running script as above and it returned empty file. I found If I change the
    $StatusNow = $AllItems.”Install State” to
    $StatusNow = $AllItems.”InstallState”

    It works fine.

  2. Hi , I am trying to install (.net Framework 3.5) feature on windows 2019 server using add role and features wizard but its giving error 0x8024401c but when i checked this error code is related to windows update and also i cant update windows it gives same error code please help me out to fix this issue

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top