ScriptSecurityWindows

Disable Windows users

Disable the local Windows accounts supplied in your username list.

See what you’re importing.

Read-only preview

PowerShell Timeout 100s Runs as Local system
<#
This resource is provided as a convenience for Level users. We cannot 
guarantee it will work in all environments. Please test before deploying 
to your production environment. We welcome contributions to our community 
library

Level Library
https://level.io/library/script-windows-delete-disable-users
#>

# Split the string into an array
$userArray = "{{UsersToDelete}}" -split ',' | ForEach-Object { $_.Trim() }

foreach ($user in $userArray) {
    # Check if the user exists (assuming we're looking for local users by username)
    if (Get-LocalUser -Name $user -ErrorAction SilentlyContinue) {
        try {
            # Disable the user account
            Disable-LocalUser -Name $user
            Write-Output "User $user has been disabled."
            
            # Commented out: To delete instead of disable, uncomment the next line
            # Remove-LocalUser -Name $user
            # Write-Output "User $user has been deleted."
        }
        catch {
            # Use double quotes with escaping for the colon
            Write-Error "Failed to disable user $user`: $_"
        }
    } else {
        Write-Output "User $user does not exist."
    }
}

Before you run it.

  1. Pair this script with a script-based monitor in Level to take immediate action when unauthorized users are detected

  2. For routine cleanups, schedule it through a Level automation to run automatically at desired intervals

  3. Test the script on a non-production environment before rolling out to ensure correct behavior

  4. Keep your “UsersToDelete” variable list accurate and current for best results

  5. Check out the Admin Compliance & Remediation Automation

More about this resource.

What it does

It uses a string of comma-separated usernames supplied through a Level script variable, converting them into an array for sequential processing. For each user, the script checks whether the account exists, then disables it by default to prevent login attempts. A simple uncommented line can switch the behavior from disabling to permanently removing the user account, granting administrators a flexible way to handle unauthorized or unneeded users. By running under System-level permissions, it ensures minimal manual intervention and reduces friction in day-to-day security operations.

When to use it
  • Promptly disabling unauthorized or forgotten user accounts on Windows
  • Automating the cleanup of unused or stale profiles for better security hygiene
  • Responding to real-time alerts from a script-based monitor in Level
  • Enforcing account-based compliance within a broader admin compliance automation
The problem it solves

This script tackles the issue of lingering or unauthorized local user accounts on Windows machines by providing a simple, automated way to disable or delete them, ultimately tightening security and preventing accidental or intentional misuse of overlooked user profiles.

Frequently asked questions.

Can I delete users instead of disabling them?

Yes, simply uncomment the “Remove-LocalUser” line and comment out the “Disable-LocalUser” line in the script.

Does this script need elevated privileges?

No, Level runs scripts under System-level permissions on Windows, so no manual elevation is required.

Will the script output if a user doesn’t exist?

Yes, you’ll see a message indicating that the user does not exist, preventing confusion or repeated attempts.

Can I process multiple users in one go?

Absolutely, you just need to provide a comma-separated list of usernames in the “UsersToDelete” variable.

Ready when you are.

No credit card. No sales call. Just sign up and start managing.