<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://tech.uvoo.io/index.php?action=history&amp;feed=atom&amp;title=Ad_clean-up</id>
	<title>Ad clean-up - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://tech.uvoo.io/index.php?action=history&amp;feed=atom&amp;title=Ad_clean-up"/>
	<link rel="alternate" type="text/html" href="https://tech.uvoo.io/index.php?title=Ad_clean-up&amp;action=history"/>
	<updated>2026-04-05T19:38:13Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.35.2</generator>
	<entry>
		<id>https://tech.uvoo.io/index.php?title=Ad_clean-up&amp;diff=5499&amp;oldid=prev</id>
		<title>Busk: Created page with &quot;# Cleanup of AD  ## Create users file account line-by-line  notepad users.txt and paste SAMAccountNames  ## Disable  disable-users-txt.ps1 ``` Import-Module ActiveDirectory $t...&quot;</title>
		<link rel="alternate" type="text/html" href="https://tech.uvoo.io/index.php?title=Ad_clean-up&amp;diff=5499&amp;oldid=prev"/>
		<updated>2025-02-05T21:26:14Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;# Cleanup of AD  ## Create users file account line-by-line  notepad users.txt and paste SAMAccountNames  ## Disable  disable-users-txt.ps1 ``` Import-Module ActiveDirectory $t...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;# Cleanup of AD&lt;br /&gt;
&lt;br /&gt;
## Create users file account line-by-line&lt;br /&gt;
&lt;br /&gt;
notepad users.txt and paste SAMAccountNames&lt;br /&gt;
&lt;br /&gt;
## Disable&lt;br /&gt;
&lt;br /&gt;
disable-users-txt.ps1&lt;br /&gt;
```&lt;br /&gt;
Import-Module ActiveDirectory&lt;br /&gt;
$textFilePath = &amp;quot;users.txt&amp;quot;&lt;br /&gt;
&lt;br /&gt;
$usernames = Get-Content -Path $textFilePath | ForEach-Object { $_.Trim() }&lt;br /&gt;
foreach ($username in $usernames) {&lt;br /&gt;
    if (-not [string]::IsNullOrWhiteSpace($username)) {&lt;br /&gt;
        try {&lt;br /&gt;
            $user = Get-ADUser -Identity $username -Properties Enabled -ErrorAction Stop&lt;br /&gt;
            if ($user.Enabled -eq $false) {&lt;br /&gt;
                Write-Output &amp;quot;Account already disabled: $username&amp;quot;&lt;br /&gt;
            } else {&lt;br /&gt;
                Write-Output &amp;quot;Disabling account: $username&amp;quot;&lt;br /&gt;
                Start-Sleep -Milliseconds 500&lt;br /&gt;
                Disable-ADAccount -Identity $username -ErrorAction Stop&lt;br /&gt;
                Write-Output &amp;quot;Successfully disabled account: $username&amp;quot;&lt;br /&gt;
            }&lt;br /&gt;
        } catch {&lt;br /&gt;
            Write-Output &amp;quot;E: Failed to disable account: $username. Error: $_&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
## Delete&lt;br /&gt;
&lt;br /&gt;
delete-users-txt.ps1&lt;br /&gt;
```&lt;br /&gt;
$textFilePath = &amp;quot;users.txt&amp;quot;&lt;br /&gt;
$usernames = Get-Content -Path $textFilePath | ForEach-Object { $_.Trim() }&lt;br /&gt;
foreach ($username in $usernames) {&lt;br /&gt;
    if (-not [string]::IsNullOrWhiteSpace($username)) {&lt;br /&gt;
        try {&lt;br /&gt;
            $user = Get-ADUser -Identity $username -Properties Enabled, MemberOf -ErrorAction Stop&lt;br /&gt;
            if ($user.Enabled -eq $true) {&lt;br /&gt;
                Write-Output &amp;quot;E: Username $username must be disabled before deletion. Skipping.&amp;quot;&lt;br /&gt;
                continue&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            $groups = $user.MemberOf&lt;br /&gt;
            if ($groups -is [System.Collections.IEnumerable] -and $groups.Count -ge 1) {&lt;br /&gt;
                foreach ($group in $groups) {&lt;br /&gt;
                    Write-Output &amp;quot;Removing user $username from group $group.&amp;quot;&lt;br /&gt;
                    Remove-ADGroupMember -Identity $group -Members $user -Confirm:$false&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            Remove-ADUser -Identity $username -Confirm:$false&lt;br /&gt;
            Write-Output &amp;quot;User $username has been removed from all groups and deleted from AD.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
        } catch {&lt;br /&gt;
            Write-Output &amp;quot;E: Failed to delete account: $username. Error: $_&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
```&lt;/div&gt;</summary>
		<author><name>Busk</name></author>
	</entry>
</feed>