Hoppa till innehållet
Deal With IT

PowerShell: Remove Windows Server role from a list of servers

A script that removes the Windows Server Backup role from every server listed in a text file, and writes the outcome for each one to a CSV.

Pontus 2 min läsning

artikelbild: terminalfönster — 16:9

This script removes the Windows Server Backup role from the servers listed in a text file. One server name on each row. The script can easily be edited to remove other roles as well.

The script

# Set the path to the text file containing the list of servers
$serverListPath = "C:\ServerList.txt"

# Set the path to the output file
$outputFilePath = "C:\Results.txt"

# Read the list of servers from the text file
$serverList = Get-Content -Path $serverListPath

# Create an empty array to store the results
$results = @()

# Loop through each server in the list
foreach ($server in $serverList) {
  # Check if the Windows Server Backup role is installed on the server
  if (Get-WindowsFeature -Name Windows-Server-Backup -ComputerName $server | Select-Object -ExpandProperty Installed) {
    # Uninstall the Windows Server Backup role
    Uninstall-WindowsFeature -Name Windows-Server-Backup -ComputerName $server

    # Add a result object to the array
    $results += [PSCustomObject]@{
      Server = $server
      Result = "Success"
    }
  }
  else {
    # Add a result object to the array
    $results += [PSCustomObject]@{
      Server = $server
      Result = "Already Uninstalled"
    }
  }
}

# Export the results to the output file
$results | Export-Csv -Path $outputFilePath -NoTypeInformation

How it works

This script reads the list of servers to remove the Windows Server Backup role from from a text file located at the specified path. It then loops through each server in the list and checks if the Windows Server Backup role is installed on the server. If the role is installed, the script uninstalls it and adds a result object to an array with the server name and the result (either "Success" or "Already Uninstalled"). After the loop is complete, the script exports the array of results to a CSV file at the specified output file path.

Note that this script assumes that you have the necessary permissions to uninstall roles and features on the target servers. You may also need to modify the script to handle any dependencies or other issues that may arise during the uninstallation process.

Dela artikeln LinkedIn

Har ni ett problem vi borde skriva om?

Hälften av artiklarna här började som en kundfråga. Ställ er fråga — svaret kanske blir nästa guide.