Manage Windows Features with PowerShell: Add Feature or Role

To get back up to speed, I want to identify File-Service sub features that are not installed.

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

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

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

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

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

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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s