Local Admin User and Group Toolkit

<#
.SYNOPSIS
Local Admin User and Group Toolkit

.DESCRIPTION
This script provides functionality to manage local administrators and groups on Windows systems.
It allows creating, deleting, and managing local admin users and groups, as well as viewing current configurations.

.NOTES
File Name      : LocalAdminToolkit.ps1
Author         : [Your Name]
Prerequisite   : PowerShell V5.1 or later, administrator rights
Version        : 1.0
Date           : [Current Date]

.EXAMPLE
.\LocalAdminToolkit.ps1
#>

# Global variables
$global:reportPath = "$env:USERPROFILE\Desktop\Local_Admin_Report_$(Get-Date -Format 'yyyyMMdd_HHmmss').html"

function Show-Menu {
    Clear-Host
    Write-Host "=== Local Admin User and Group Toolkit ===" -ForegroundColor Cyan
    Write-Host "1. View Local Administrators"
    Write-Host "2. Create New Local Admin User"
    Write-Host "3. Delete Local Admin User"
    Write-Host "4. Add User to Administrators Group"
    Write-Host "5. Remove User from Administrators Group"
    Write-Host "6. View All Local Groups"
    Write-Host "7. Create New Local Group"
    Write-Host "8. Delete Local Group"
    Write-Host "9. Add User to Group"
    Write-Host "10. Remove User from Group"
    Write-Host "11. Generate HTML Report"
    Write-Host "12. Exit"
}

function View-LocalAdministrators {
    Write-Host "`nViewing Local Administrators..." -ForegroundColor Yellow
    $admins = Get-LocalGroupMember -Group "Administrators"
    $admins | Format-Table Name, PrincipalSource
    return $admins
}

function Create-NewLocalAdminUser {
    $username = Read-Host "Enter the new admin username"
    $password = Read-Host "Enter the password" -AsSecureString
    $fullName = Read-Host "Enter the full name (optional)"
    $description = Read-Host "Enter a description (optional)"

    try {
        New-LocalUser -Name $username -Password $password -FullName $fullName -Description $description -ErrorAction Stop
        Add-LocalGroupMember -Group "Administrators" -Member $username
        Write-Host "User $username created and added to Administrators group." -ForegroundColor Green
    }
    catch {
        Write-Host "Error creating user: $_" -ForegroundColor Red
    }
}

function Delete-LocalAdminUser {
    $username = Read-Host "Enter the username to delete"
    
    try {
        Remove-LocalUser -Name $username -ErrorAction Stop
        Write-Host "User $username deleted." -ForegroundColor Green
    }
    catch {
        Write-Host "Error deleting user: $_" -ForegroundColor Red
    }
}

function Add-UserToAdministrators {
    $username = Read-Host "Enter the username to add to Administrators group"
    
    try {
        Add-LocalGroupMember -Group "Administrators" -Member $username -ErrorAction Stop
        Write-Host "User $username added to Administrators group." -ForegroundColor Green
    }
    catch {
        Write-Host "Error adding user to Administrators group: $_" -ForegroundColor Red
    }
}

function Remove-UserFromAdministrators {
    $username = Read-Host "Enter the username to remove from Administrators group"
    
    try {
        Remove-LocalGroupMember -Group "Administrators" -Member $username -ErrorAction Stop
        Write-Host "User $username removed from Administrators group." -ForegroundColor Green
    }
    catch {
        Write-Host "Error removing user from Administrators group: $_" -ForegroundColor Red
    }
}

function View-AllLocalGroups {
    Write-Host "`nViewing All Local Groups..." -ForegroundColor Yellow
    $groups = Get-LocalGroup
    $groups | Format-Table Name, Description
    return $groups
}

function Create-NewLocalGroup {
    $groupName = Read-Host "Enter the new group name"
    $description = Read-Host "Enter a description (optional)"

    try {
        New-LocalGroup -Name $groupName -Description $description -ErrorAction Stop
        Write-Host "Group $groupName created." -ForegroundColor Green
    }
    catch {
        Write-Host "Error creating group: $_" -ForegroundColor Red
    }
}

function Delete-LocalGroup {
    $groupName = Read-Host "Enter the group name to delete"
    
    try {
        Remove-LocalGroup -Name $groupName -ErrorAction Stop
        Write-Host "Group $groupName deleted." -ForegroundColor Green
    }
    catch {
        Write-Host "Error deleting group: $_" -ForegroundColor Red
    }
}

function Add-UserToGroup {
    $username = Read-Host "Enter the username to add"
    $groupName = Read-Host "Enter the group name"
    
    try {
        Add-LocalGroupMember -Group $groupName -Member $username -ErrorAction Stop
        Write-Host "User $username added to group $groupName." -ForegroundColor Green
    }
    catch {
        Write-Host "Error adding user to group: $_" -ForegroundColor Red
    }
}

function Remove-UserFromGroup {
    $username = Read-Host "Enter the username to remove"
    $groupName = Read-Host "Enter the group name"
    
    try {
        Remove-LocalGroupMember -Group $groupName -Member $username -ErrorAction Stop
        Write-Host "User $username removed from group $groupName." -ForegroundColor Green
    }
    catch {
        Write-Host "Error removing user from group: $_" -ForegroundColor Red
    }
}

function Generate-HTMLReport {
    Write-Host "`nGenerating HTML Report..." -ForegroundColor Yellow
    
    $admins = View-LocalAdministrators
    $groups = View-AllLocalGroups

    $reportContent = @"
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Local Admin and Group Report</title>
    <style>
        body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; max-width: 1200px; margin: 0 auto; padding: 20px; }
        h1, h2, h3 { color: #0078D4; }
        table { border-collapse: collapse; width: 100%; margin-bottom: 20px; }
        th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
        th { background-color: #f2f2f2; }
    </style>
</head>
<body>
    <h1>Local Admin and Group Report</h1>
    <p>Generated on: $(Get-Date)</p>

    <h2>Local Administrators</h2>
    $($admins | ConvertTo-Html -Fragment)

    <h2>All Local Groups</h2>
    $($groups | ConvertTo-Html -Fragment)
</body>
</html>
"@

    $reportContent | Out-File -FilePath $global:reportPath
    Write-Host "Report generated and saved to: $global:reportPath" -ForegroundColor Green
}

# Main program loop
do {
    Show-Menu
    $choice = Read-Host "`nEnter your choice (1-12)"

    switch ($choice) {
        "1" { View-LocalAdministrators }
        "2" { Create-NewLocalAdminUser }
        "3" { Delete-LocalAdminUser }
        "4" { Add-UserToAdministrators }
        "5" { Remove-UserFromAdministrators }
        "6" { View-AllLocalGroups }
        "7" { Create-NewLocalGroup }
        "8" { Delete-LocalGroup }
        "9" { Add-UserToGroup }
        "10" { Remove-UserFromGroup }
        "11" { Generate-HTMLReport }
        "12" { Write-Host "Exiting program..." -ForegroundColor Yellow; break }
        default { Write-Host "Invalid choice. Please try again." -ForegroundColor Red }
    }

    if ($choice -ne "12") {
        Read-Host "`nPress Enter to continue..."
    }
} while ($choice -ne "12")

This Local Admin User and Group Toolkit includes:

  1. A menu-driven interface for easy navigation.
  2. Functions to manage local administrators and groups:
    • View local administrators
    • Create new local admin users
    • Delete local admin users
    • Add users to the Administrators group
    • Remove users from the Administrators group
    • View all local groups
    • Create new local groups
    • Delete local groups
    • Add users to groups
    • Remove users from groups
  3. HTML report generation for easy sharing and viewing of current configurations.

Key features:

  • Comprehensive management of local users and groups
  • Easy-to-use interface for common administrative tasks
  • Detailed error handling for each operation
  • HTML report generation for documentation purposes

Important notes:

  1. This script requires administrator rights to run effectively.
  2. Be cautious when deleting users or groups, as this action cannot be undone.
  3. Ensure you have proper authorization before modifying user accounts or group memberships.
  4. It’s recommended to test this script in a non-production environment first.

To use this script:

  1. Run PowerShell as an administrator
  2. Navigate to the directory containing the script
  3. Execute the script: .\LocalAdminToolkit.ps1
  4. Follow the on-screen menu to perform desired actions

This toolkit provides a convenient way to manage local administrators and groups on Windows systems, which can be particularly useful for system administrators managing multiple machines or performing routine user management tasks.