PowerShell, Remoting, Scripting, Server 2008 R2, Windows
comments 12

PowerShell to list / add / remove Windows roles & features remotely

Lot of my R & D work at home happens on a couple of physical systems and lot of virtual machines hosted on Hyper-V. One thing that I regularly do is to rebuild lot of these machines and in that process, I add/remove Windows OS features many times. One default setting I have on all my systems is PowerShell remoting. This helps me access any system from a central location to add / remove these Windows features. I have been using PowerShell remoting combined with Server Manager cmdlets in Windows Server 2008 R2. But, every time I want to enable/disable a feature, there is lot of typing. So, I ended up writing my own wrapper for doing this remotely using three functions or cmdlets — whatever you call it.

These functions have been tested on Windows Server 2008 R2 and ServerCore R2 OS. ServerCore is a bit tricky though. We don’t have PowerShell enabled by default. So, we have to manually enable Windows PowerShell feature using OCSetup. Only then, we can use this script for enabling/disabling features on a remote ServerCore system.

Here is the script:

So, to be able to use this script, you should have PowerShell remoting enabled on all remote computers. Once, you have this done, copy the contents of above script to a file and load it using Import-Module. Now, you can get a list of all roles/features by using Get-OSFeature cmdlet.

Once, you know the name of the feature you want to enable, you can use Add-OSFeature to install the feature.

If you want to remove a feature, use Remove-OSFeature.

The following screen capture shows this script in action.

Feature Install

Feature Install

I have also enabled verbose output and you can see detailed information such as the script block that is getting passed to the remote system, etc. All you have to do it is, use -Verbose.

Verbose output

Verbose output

Do let me know if you have or know a better way to do this. This script is in the basic form that I am using right now. This can certainly get better. There are few TODO.

1. Add support for -Credential to authenticate to remote PowerShell session.

2. Add support for persistent sessions.

3. Add “check install status before removing a role or feature.

Filed under: PowerShell, Remoting, Scripting, Server 2008 R2, Windows

by

Ravikanth is a principal engineer and the lead architect for Microsoft and VMware virtualized and hybrid cloud solutions within the Infrastructure Solutions Group at Dell EMC. He is a multi-year recipient of Microsoft Most Valuable Professional (MVP) award in Windows PowerShell (CDM) and Microsoft Azure. Ravikanth is the author of Windows PowerShell Desired State Configuration Revealed (Apress) and leads Bangalore PowerShell and Bangalore IT Pro user groups. He can be seen speaking regularly at local user group events and conferences in India and abroad.