Cyber Security

List all Server Roles from all Windows Servers with PowerShell

So you are about to document your network and os environment? You don’t want to purchase 3rd party software? You want to list all server roles from all servers in your network? Here you go … with PowerShell, what else? 😉

What can I expect from this blog post? Take a look at the picture below. If this is what you are looking for, read on …

Windows Server supports the installation of so called features and roles. Features are only locally relevant. Roles are network roles, for example DHCP, DNS and so on.

You can copy & paste the code right here, or you can download the entire script you will find at the end of this post and in my downloads section.

Code: Retrieve all Server Roles

Here it is. Copy and paste the lines into PowerShell ISE or Visual Studio Code and pull the trigger, usually hit F5.

By the way: This script was tested in PowerShell 5.1 and PowerShell 7.


# .SYNOPSIS
# This script collects all installed Server Roles and Features
# from all domain-joined Windows Servers.

# .DESCRIPTION
# The result is displayed in the console and a report will be
# saved in $HOME\ServerRoles"Date".txt.
# The file will open automatically.

# .NOTES
# Run this script with administrative privileges
# on a domain-controller of your domain.
# Supported PowerShell versions: PowerShell 5.1, PowerShell 7

# .AUTHOR
# Patrick Gruenauer | Microsoft MVP PowerShell [2018-2020]

# .WEB
# https://sid-500.com

############################ CODE ###############################

### Retrieving all Servers by Name

$ErrorActionPreference = "SilentlyContinue"

$servers = (Get-ADComputer -Properties operatingsystem `
-Filter 'operatingsystem -like "*server*" -and enabled -eq "true"').Name

### Collection Point

$result = @()

### Ping all Member-Server and if ping is successful run
# Get-WindowsFeature

foreach ($item in $servers) {

$test = Test-Connection $item -Count 1

### Providing PowerShell 7 and 5.1 compatibility in terms of return code

If ($test.Status -eq 'Success' -or $test.StatusCode -eq '0')
{

$roles = Get-WindowsFeature -ComputerName $item |
Where-Object InstallState -EQ 'Installed'

### Write into the collection point

$result += New-Object -TypeName PSObject -Property ([ordered]@{

'Server' = $item
'Roles' = $roles.Name -join "`r`n"
### Escape Character CarriageReturn (`r) + NewLine (`n)

}
)

}
}

### Generating Outputs

# Collecting Point: Console Output

Write-Output $result | Format-Table -Wrap

# Text File Output

$date = Get-Date -Format MM.dd.yyyy
$result | Format-Table -Wrap |
Out-File "$HOME\ServerRoles$date.txt"
Write-Warning "Output file ServerRoles$date.txt generated in $HOME ... Opening file ..."

# Opening File

Start-Process $HOME\"ServerRoles$date.txt"

As mentioned, copy the code above into your PowerShell editor of your choice or download the script in the Downloads section of my website.

For retrieving server roles on individual servers see Windows Server: List all installed Roles and Features using PowerShell

Have fun documenting!

5 replies »

  1. Hey Patrick, it looks like the script only works on 2012/R2 and newer since 2008 servers don’t natively recognize the get-WindowsFeature cmdlet. Do you have a recommendation on how we could modify the script to include 2008 servers? Or maybe a way we could modify the script to run after pulling 2012 and newer inventory. Thanks!

    Like

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.