Solved

User account maintenance PowerShell for Window 2003

Posted on 2014-01-16
4
704 Views
Last Modified: 2014-01-18
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
Comment
Question by:AXISHK
  • 2
  • 2
4 Comments
 
LVL 5

Expert Comment

by:Recept
Comment Utility
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
 

Author Comment

by:AXISHK
Comment Utility
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
 
LVL 5

Accepted Solution

by:
Recept earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:AXISHK
Comment Utility
Tks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
This article will help you understand what HashTables are and how to use them in PowerShell.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now