Learn What IT Pros Need to Know About Windows 11 - August 24th at 1 PM ET! Learn What IT Pros Need to Know About Windows 11 - August 24th at 1 PM ET!
PowerShell

Manage Windows Features with PowerShell: Add Feature or Role

In part one of this two-part series, I showed you how to configure and manage Windows features using Windows PowerShell and the ServerManager module. Naturally, the next step is to add a feature or role. We’ll pick up where the last article left off in a remote PowerShell session.

To get back up to speed, I want to identify File-Service sub features that are not installed.
petri.com/wp-admin[chi-fp01]: PS C:\> import-module ServerManager
[chi-fp01]: PS C:\> Get-WindowsFeature file-services | select -expandproperty SubFeatures | get-WindowsFeature | where {-Not $_.Installed}

Display Name Name
———— —-
[ ] Distributed File System FS-DFS
[ ] File Server Resource Manager FS-Resource-Manager
[ ] Services for Network File System FS-NFS-Services
[ ] Windows Search Service FS-Search-Service
[ ] Windows Server 2003 File Services FS-Win2003-Services
[ ] BranchCache for network files FS-BranchCache

I’m betting you can figure out the name of the cmdlet to add a Windows feature.
petri.com/wp-admin[chi-fp01]: PS C:\> Add-WindowsFeature FS-DFS -WhatIf
What if: Checking if running in ‘WhatIf’ Mode.
What if: Performing operation “Add-WindowsFeature” on Target “[File Services] Distributed File System”.
What if: Performing operation “Add-WindowsFeature” on Target “[File Services] DFS Replication”.
What if: Performing operation “Add-WindowsFeature” on Target “[File Services] DFS Namespaces”.
What if: This server may need to be restarted after the installation completes.

Sponsored Content

Read the Best Personal and Business Tech without Ads

Staying updated on what is happening in the technology sector is important to your career and your personal life but ads can make reading news, distracting. With Thurrott Premium, you can enjoy the best coverage in tech without the annoying ads.

Success Restart Needed Exit Code Feature Result
——- ————– ——— ————–
True Maybe Success {}

[chi-fp01]: PS C:\>
Using –Whatif is a smart idea here as I can see that some additional features will also be installed. When I’m ready, I can re-run this command without –Whatif.
petri.com/wp-admin[chi-fp01]: PS C:\> Add-WindowsFeature FS-DFS

Success Restart Needed Exit Code Feature Result
——- ————– ——— ————–
True No Success {DFS Replication, DFS Namespaces}
In this particular case, no reboot was required. If it had been, I could reboot the server whenever I wanted with Restart-Computer, or wait until the next scheduled maintenance window. If you know in advance that installing a feature or role will require a reboot and you don’t mind rebooting immediately, use the –Restart parameter. If no reboot is required, then the parameter should be ignored.

Some features or roles include a number of sub-roles, as we’ve seen. You can either install a group of roles or you might decide to install a feature or role with all of its subroles.
petri.com/wp-admin[chi-fp01]: PS C:\> Add-WindowsFeature SNMP-Services -IncludeAllSubFeature -Restart -WhatIf
What if: Checking if running in ‘WhatIf’ Mode.
What if: Performing operation “Add-WindowsFeature” on Target “[SNMP Services] SNMP WMI Provider”.
What if: Performing operation “Add-WindowsFeature” on Target “[SNMP Services] SNMP Service”.
What if: This server may need to be restarted after the installation completes.

Success Restart Needed Exit Code Feature Result
——- ————– ——— ————–
True Maybe Success {}

[chi-fp01]: PS C:\>
I don’t need this feature but I wanted to show you how the parameters work. If you attempt to install a group of features that happen to have other requirements, you will be prompted to install them as well. In an automated solution, you will want to identify these situations ahead of time and install all the necessary features at once or in the right order.

The last management task is removing a feature. On my server, someone has mistakenly installed Telnet tools.
petri.com/wp-admin[chi-fp01]: PS C:\> get-windowsfeature telnet*

Display Name Name
———— —-
[X] Telnet Client Telnet-Client
[X] Telnet Server Telnet-Server
I want to delete them. The Remove-WindowsFeature cmdlet doesn’t support wildcards so I would have to type the names out, separated by commas. I’d rather have PowerShell do the work.
petri.com/wp-admin[chi-fp01]: PS C:\> Get-WindowsFeature Telnet* | Remove-WindowsFeature -whatif
What if: Checking if running in ‘WhatIf’ Mode.
What if: Performing operation “Remove-WindowsFeature” on Target “[Telnet Server] Telnet Server”.
What if: Performing operation “Remove-WindowsFeature” on Target “[Telnet Client] Telnet Client”.
What if: This server may need to be restarted after the removal completes.

Success Restart Needed Exit Code Feature Result
——- ————– ——— ————–
True Maybe Success {}

[chi-fp01]: PS C:\>
When I run the command without –Whatif, the features will be removed. There are any number of ways you might leverage this type of pipelined expression to remove non-standard features or to insure servers meet your build standards. And to repeat, we did all of this without any scripting. All we did was run some commands from a PowerShell prompt.

If you are interested in learning about other ways to manage your servers with Windows PowerShell, I hope you’ll take a look at my video training course, Windows Server 2008 PowerShell Training.

Related Topics:

BECOME A PETRI MEMBER:

Don't have a login but want to join the conversation? Sign up for a Petri Account

Register
Comments (8)

8 responses to “Manage Windows Features with PowerShell: Add Feature or Role”

  1. Manage Windows Features with PowerShell Part 2 | Tech Blogs

    [...] Here is the original post: Manage Windows Features with PowerShell Part 2 [...]
  2. Manage Windows Features with PowerShell

    [...] Server 2008 R2 system using Windows PowerShell and the ServerManager module. In part two, I'll add a feature or role to Windows features with PowerShell. Help Has [...]
  3. Edwin

    Thanks, very clear and helpful. The thing I want to do is remove ALL roles and features that are not installed (in other words; all the roles and features that appear as "Available"). They are hogging like 5 to 10 GB of disk space on my C: drive. Do you have a smart way to accomplish this?
  4. Jeffery Hicks

    Edwin, Thanks for taking the time to comment. Sure, this can be done without too much effort. First, you need to identify all the features currently NOT installed. get-windowsfeature | where {-Not $_.Installed} If you are happy with that, repeat the command and pipe to Uninstall-WindowsFeature.-remove The -Remove is what gets rid of the files and should free up some disk space.
  5. Edwin

    Thank you, that worked great! The only "downside" is that I only won 1,5 GB of free disk space on a clean 2012 R2 installation. I hoped it was going to be more :-)
  6. Edwin

    OK another question for you: How do I add all the removed binaries back? :-) I know you can do "Install-WindowsFeature" and specify the -source parameter to install the binaries and the feature together ("Add-WindowsFeature" in Server 2008), but there is no special command to install only the binaries (afaik). This could come in handy if I want to prepare an installation in advance.
  7. Jurgen Cauwenbergh

    How can one specify installation parameters ? For example, when manually installing the Search Service, i can specify the index location (defaulted to C:ProgramDataMicrosoft) Import-Module servermanager Add-windowsfeature FS-Search-Service Can't I specify another location using powershell ?
  8. Jeffery Hicks

    You can't customize the feature installation. I don't think the GUI let's you nor does PowerShell.

Leave a Reply

Register for Advanced Microsoft 365 Day!

GET-IT: Advanced Microsoft 365 1-Day Virtual Conference - Live August 24th!

Join us on Tuesday, August 24th and hear from Microsoft MVPs and industry experts about how to take advantage of Microsoft 365 at a technical level and dive deep into the features and functionality that will make your environment more secure and compliant.

RSVP Now

Sponsored By