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.

  • Pingback: Tweets that mention Blog: PowerShell to list / add / remove Windows roles & features remotely -- Topsy.com()

  • JB

    Is it possible to have this script get the computer name(s) from a text file so it can do more than one at a time

  • Yes, we can just make the $computerName parameter to accept an array of values. For example,
    Param(
    [Parameter(Mandatory=$true)]
    [String[]]$computername,
    )

    Once, this is done, we need to put a foreach loop for $computerName in the script and perform the action. The way you call the cmdlet changes to Add-OSFeature -computerName (get-Content computers.txt) -featureName NLB

    Or

    You can use the value from pipeline attribute and not change the script at all. In this case,
    Param
    (
    [parameter(Mandatory=$true,
    ValueFromPipeline=$true)]
    $ComputerName
    )
    .Now, we can call the cmdlet using Get-Content Computers.txt | Foreach-Object { Add-OSFeature -computerName $_ -FeatureName NLB }

    I will update the above script to do both and post it here again.

  • Silentm

    This script works great! You mentioned updating it so it can be run against multiple computers. Have you had a chance to get this done?

  • I am planning to release all these utility scripts at http://poshutils.codeplex.com. Please look out for the announcement.

  • Anonymous

    THANK YOU!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    This came in right on time

  • Comfortablynumb

    it would be great if this could be done on multiple computers from a text file?

  • it helped. thank you!

  • Asif

    How about using the Invoke-Command feature?
    Invoke-Command -ComputerName (Get-Content Computers.txt) -Command { Import-Module ServerManager;Add-WindowsFeature -Name Feature1,Feature2,Feature3 }This will connect to each computer, add the requested features and report back with a success or failure.

  • Sure, in fact, if you look at my script, that is what I am doing. The script I posted here makes it easy for those who don’t want to go through the Invoke-Command. And, it gives the same uniform access to local / remote feature enablement.

  • MattM

    Thank you very much.

  • rohan jannu

    this is very good script i found ever …………….

    but can you please create script for install or uninstall EXE to remote system