close

Home

PowerShell

Manage Windows Features with PowerShell: Add Feature or Role

Author avatar - Jeff Hicks

Jeff Hicks

|

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.
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.

Log in to save content to your profile.

Username/Email

Password

Login

Article saved!

Access saved content from your profile page. View Saved