Automating Netsweeper Removal and Replacement with a Powerful PowerShell Script

0
Automating Netsweeper Removal

Have you ever needed to remove all traces of the Netsweeper software from your system and replace it with a new version? Look no further! We have developed a powerful PowerShell script that automates the entire process. This blog post will discuss how the script works, what it does, and how to use it to make your life easier.

The PowerShell script we’ve designed offers a comprehensive solution for removing Netsweeper programs, services, and related data from a Windows system. After the removal process, it silently installs a new MSI file with an accompanying transform. Here’s an overview of how the script operates:

  1. First, the script checks if it’s running with Administrator privileges. If not, it will prompt the user to run it as an Administrator, ensuring it has the necessary permissions to perform the removal and installation tasks.
  2. Next, it locates and stops any services associated with Netsweeper or those named “NSFX Service”. The script iterates through each service, stopping it, disabling it, and removing it using the Remove-WmiObject cmdlet.
  3. The script then searches for programs with “netsweeper” in their names or publishers using the Get-WmiObject cmdlet. It silently uninstalls each identified program using the msiexec.exe tool with the /x, /qn, and /norestart arguments.
  4. After uninstalling the programs, the script removes any files and folders associated with Netsweeper. It searches through common system folders, like Program Files and AppData, and deletes any items containing “netsweeper” in their names.
  5. Finally, the script silently installs a new MSI and specified transform files. The installation is performed using the msiexec.exe tool with the /i, TRANSFORMS=, /qn, and /norestart arguments.

This PowerShell script is perfect for system administrators, IT professionals, or even tech-savvy users who need to replace Netsweeper software quickly and efficiently. With this script, you can save time and reduce the likelihood of errors or oversights during manual removal and installation.

# Run this script with Administrator privileges
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
    Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
    exit
}

try {
    # Check if the MSI file with the same version is already installed
    $existingProduct = Get-WmiObject -Class Win32_Product | Where-Object { $_.Name -match "Netsweeper Client Filter" -and $_.Version -match "10.50.53.53" }
    if ($null -ne $existingProduct) {
        Write-Host "Netsweeper version '10.50.53.53' is already installed. No further action performed."
        exit
    }
}
catch {
    Write-Error "Error checking for existing Netsweeper version: $_"
    exit
}

try {
    # Find and stop services associated with "netsweeper" or named "NSFX Service"
    $netsweeperServices = Get-Service | Where-Object { $_.Name -match 'netsweeper' -or $_.DisplayName -match 'netsweeper' -or $_.DisplayName -eq 'NSFX Service' }
    foreach ($service in $netsweeperServices) {
        Write-Host "Stopping and removing service $($service.DisplayName)..."
        $service | Stop-Service -ErrorAction Stop
        $service | Set-Service -StartupType Disabled -ErrorAction Stop
        $service | Remove-WmiObject -Class Win32_Service -ErrorAction Stop
    }
}
catch {
    Write-Error "Error stopping and removing services: $_"
    exit
}

try {
    # Find programs with "netsweeper" in their names or publishers
    $netsweeperPrograms = Get-WmiObject -Class Win32_Product | Where-Object { $_.Name -match 'netsweeper' -or $_.Vendor -match 'netsweeper' }
    foreach ($program in $netsweeperPrograms) {
        Write-Host "Uninstalling $($program.Name) silently..."
        $uninstallString = "/x $($program.IdentifyingNumber) /qn /norestart"
        Start-Process -FilePath 'msiexec.exe' -ArgumentList $uninstallString -Wait -NoNewWindow
    }
}
catch {
    Write-Error "Error uninstalling Netsweeper programs: $_"
    exit
}

try {
    # Remove files and folders associated with "netsweeper"
    $locations = @(
        "$env:ProgramFiles",
        "$env:ProgramFiles (x86)",
        "$env:ProgramData",
        "$env:LocalAppData",
        "$env:AppData"
    )

    foreach ($location in $locations) {
        Get-ChildItem -Path $location -Recurse -ErrorAction SilentlyContinue | Where-Object { $_.Name -match 'netsweeper' } | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
    }
}
catch {
    Write-Error "Error removing Netsweeper files and folders: $_"
    exit
}

try {
    # Specify the path to the MSI and MST files
    $msiFilePath = "Netsweeper Client Filter - 10.50.53.53 win64.msi"
    $mstFilePath = "EAT-NSW.mst"

    # Silently install the MSI file with the specified transform
    Write-Host "Installing '$msiFilePath' silently with transform '$mstFilePath'..."
    $installString = "/i `"$msiFilePath`" TRANSFORMS=`"$mstFilePath`" /qn /norestart"
    Start-Process -FilePath 'msiexec.exe' -ArgumentList $installString -Wait -NoNewWindow
}
catch {
    Write-Error "Error installing new Netsweeper Client Filter: $_"
    exit
}

Write-Host "Netsweeper programs, services, and related data have been removed. New MSI file has been installed with the specified transform."

Found priceless insights in this blog? Support the author’s creativity – buy them a coffee!

Leave a Reply

Your email address will not be published. Required fields are marked *