PowerShell - Install RSAT Features on Demand for 1809+

Discussion in 'Scripting' started by GodHand, Feb 6, 2020.

  1. GodHand

    GodHand MDL Addicted

    Jul 15, 2016
    534
    926
    30
    This is an advanced PowerShell script that will install or uninstall the RSAT Features on Demand for Windows 10 1809+

    For uninstalling, the function will first remove the core RSAT Features on Demand first and then the dependency RSAT Features on Demand second to ensure no dependency errors are returned that prevent its uninstallation.

    We use the registry to bypass the usage of WSUS for the installation of RSAT until it is released to the WSUS Update Channel.

    To run, simply save the code to a .ps1 file, open an elevated PowerShell session and call the script using either the -Install or -Uninstall switches. Here, we're assuming the name for the saved .ps1 file is 'RSAT.ps1':

    Code:
    <#
        .SYNOPSIS
            Installs the Features on Demand Remote Server Administration Tools (RSAT) Windows Capabilities on Windows 10 version 1809 and above.
            Uninstalls the Features on Demand Remote Server Administration Tools (RSAT) Windows Capabilities on Windows 10 version 1809 and above.
    #>
    [CmdletBinding(DefaultParameterSetName = 'Install')]
    Param
    (
        [Parameter(ParameterSetName = 'Install')]
        [Switch]$Install,
        [Parameter(ParameterSetName = 'Uninstall')]
        [Switch]$Uninstall
    )
    
    Begin
    {
        $DefaultErrorActionPreference = $ErrorActionPreference
        $ProgressPreference = 'SilentlyContinue'
        $ErrorActionPreference = 'SilentlyContinue'
    
        # Ensure we are running with administrative privileges.
        If (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
        {
            Write-Warning "Elevation is required to install RSAT. Please relaunch this script as an administrator."
            Start-Sleep 3
            Exit
        }
    
        # Ensure we are running on a supported Windows 10 build.
        If ((Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -lt 17663)
        {
            Write-Warning "Build is not supported: [$($(Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber))]"
            Start-Sleep 3
            Exit
        }
    
        If ($PSBoundParameters.Install)
        {
            # Save the current WSUS registry property so we can return it back to its default value.
            $WSUSUpdates = @{ WSUSProperty = (Get-ItemProperty -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU -ErrorAction Ignore | Select-Object -ExpandProperty UseWUServer -ErrorAction Ignore); UsesWSUS = $null }
        }
    }
    Process
    {
        Clear-Host
    
        If ($PSCmdlet.ParameterSetName -eq 'Install')
        {
            # Bypass the usage of WSUS for the installation of Remote Server Administration Tools until it is released to the WSUS Update Channel.
            If ($null -ne $WSUSUpdates.WSUSProperty)
            {
                Set-ItemProperty -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU -Name UseWUServer -Value 0 -Type DWord
                $WSUSUpdates.UsesWSUS = $false
            }
    
            # Install the Remote Server Administration Tools.
            $RSAT = Get-WindowsCapability -Online -Name RSAT* | Where-Object -Property State -EQ NotPresent
            If ($RSAT)
            {
                Write-Verbose "Installing Remote Server Administration Tools." -Verbose
                Write-Output ''
                $RSAT | ForEach-Object -Process {
                    Write-Host $($PSItem.DisplayName) -ForegroundColor Cyan
                    [Void](Add-WindowsCapability -Online -Name $($PSItem.Name))
                }
                Write-Output ''
                Write-Verbose "Remote Server Administration Tools installation complete." -Verbose
            }
    
            If ($WSUSUpdates.UsesWSUS -eq $false)
            {
                # Return the WSUS registry property back to its default value.
                Set-ItemProperty -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU -Name UseWUServer -Value $WSUSUpdates.WSUSProperty -Type DWord
                $WSUSUpdates.UsesWSUS = $null
            }
        }
        ElseIf ($PSCmdlet.ParameterSetName -eq 'Uninstall')
        {
            # Uninstall the core Remote Server Administration Tools first to prevent errors when uninstalling dependency Remote Server Administration Tools.
            $RSATCore = Get-WindowsCapability -Online -Name RSAT* | Where-Object { $PSItem.State -eq "Installed" -and $PSItem.Name -notlike "Rsat.ServerManager.*" -and $PSItem.Name -notlike "Rsat.GroupPolicy.*" -and $PSItem.Name -notlike "Rsat.ActiveDirectory.*" }
            If ($RSATCore)
            {
                Write-Verbose "Uninstalling Core Remote Server Administration Tools." -Verbose
                Write-Output ''
                $RSATCore | ForEach-Object -Process {
                    Write-Host $($PSItem.DisplayName) -ForegroundColor Cyan
                    [Void](Remove-WindowsCapability -Online -Name $($PSItem.Name))
                }
                Write-Output ''
            }
    
            If ($null -eq (Get-WindowsCapability -Online -Name RSAT* | Where-Object { $PSItem.State -eq "Installed" -and $PSItem.Name -notlike "Rsat.ServerManager.*" -and $PSItem.Name -notlike "Rsat.GroupPolicy.*" -and $PSItem.Name -notlike "Rsat.ActiveDirectory.*" }))
            {
                # Now that the core Remote Server Administration Tools have been uninstalled, we can uninstall the remaining dependency Remote Server Administration Tools.
                $RSATDependencies = Get-WindowsCapability -Online -Name RSAT* | Where-Object -Property State -EQ Installed
                If ($RSATDependencies)
                {
                    Write-Verbose "Uninstalling Dependency Remote Server Administration Tools." -Verbose
                    Write-Output ''
                    $RSATDependencies | ForEach-Object -Process {
                        Write-Host $($PSItem.DisplayName) -ForegroundColor Cyan
                        [Void](Remove-WindowsCapability -Online -Name $($PSItem.Name))
                    }
                    Write-Output ''
                    Write-Verbose "Remote Server Administration Tools uninstallation complete." -Verbose
                }
            }
        }
    }
    End
    {
        $ErrorActionPreference = $DefaultErrorActionPreference
    }
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...