Coming Soon: GET:IT Endpoint Management 1-Day Conference on September 28th at 9:30 AM ET Coming Soon: GET:IT Endpoint Management 1-Day Conference on September 28th at 9:30 AM 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

Say Goodbye to Traditional PC Lifecycle Management

Traditional IT tools, including Microsoft SCCM, Ghost Solution Suite, and KACE, often require considerable custom configurations by T3 technicians (an expensive and often elusive IT resource) to enable management of a hybrid onsite + remote workforce. In many cases, even with the best resources, organizations are finding that these on-premise tools simply cannot support remote endpoints consistently and reliably due to infrastructure limitations.

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

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

  3. 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 :-)

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

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

Leave a Reply