• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 732
  • Last Modified:

User account maintenance PowerShell for Window 2003

What's the powershell to run on Window 2003

1. to list all users that have been disabled over 1 months
2. remove the user accounts that have been disabled over 2 months.

Tks
0
AXISHK
Asked:
AXISHK
  • 2
  • 2
1 Solution
 
ReceptCommented:
Unfortunately there is no AD attributed for when an account was disabled so all we can do is check for disabled accounts which have not been modified in the last X days.


1. List all users that have been disabled over 1 month (30 days)

The Native AD Powershell way (a Windows 2008 Domain Controller required);

Get-ADUser -LDAPFilter {(useraccountcontrol:1.2.840.113556.1.4.803:=2)} -Properties whenChanged | Where-Object {$_.whenChanged -gt (Get-Date).AddDays(-30)} | Select-Object Name, whenChanged

Open in new window


The Quest AD Cmdlets way (Quest AD Cmdlets required but works on 2003);

Get-QADUser -Disabled | Where-Object {$_.whenChanged -gt (get-date).AddDays(-30)} | Select-Object Name, whenChanged

Open in new window



2. Remove the user accounts that have been disabled over 2 months (60 days)

The Native AD Powershell way;
Get-ADUser -LDAPFilter {(useraccountcontrol:1.2.840.113556.1.4.803:=2)} -Properties whenChanged | Where-Object {$_.whenChanged -gt (Get-Date).AddDays(-60)} | Remove-ADUser

Open in new window


The Quest AD Cmdlets way;
Get-QADUser -Disabled | Where-Object {$_.whenChanged -gt (get-date).AddDays(-60)} | Remove-QADObject

Open in new window



Please test the above before running on a Production environment. You can do this by adding the -WhatIf command after the Remove-ADUser and Remove-QADObject commands.

For example
Get-QADUser -Disabled | Where-Object {$_.whenChanged -gt (get-date).AddDays(-60)} | Remove-QADObject -WhatIf

Open in new window

0
 
AXISHKAuthor Commented:
Get-ADUser -LDAPFilter {(useraccountcontrol:1.2.840.113556.1.4.803:=2)} -Properties whenChanged | Where-Object {$_.whenChanged -gt (Get-Date).AddDays(-30)} | Select-Object Name, whenChanged

Can I add a criteria to filter out the account that has been disabled?
Can I redirect the result into a CSV file ?
Can I include the location (OU) in the result ?

Tks
0
 
ReceptCommented:
Can I add a criteria to filter out the account that has been disabled?
This is already happening, this part of the command performs an LDAP filter to only return disabled accounts.
-LDAPFilter {(useraccountcontrol:1.2.840.113556.1.4.803:=2)}

Open in new window


Can I redirect the result into a CSV file ?
Absolutely, we just need to pipe the results into the Export-Csv command

Can I include the location (OU) in the result?
We sure can, we just need to include a cleaned up version of the DistinguishedName field


With the amendments the updated command is
Get-ADUser -LDAPFilter {(useraccountcontrol:1.2.840.113556.1.4.803:=2)} -Properties whenChanged | Where-Object {$_.whenChanged -gt (Get-Date).AddDays(-30)} | Select-Object Name, whenChanged, @{name="OU";expression={($_.DistinguishedName -split ",",2)[1]}} | Export-Csv C:\blah.csv

Open in new window

0
 
AXISHKAuthor Commented:
Tks
0

Featured Post

A Cyber Security RX to Protect Your Organization

Join us on December 13th for a webinar to learn how medical providers can defend against malware with a cyber security "Rx" that supports a healthy technology adoption plan for every healthcare organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now