?
Solved

delete old users in AD

Posted on 2014-09-24
3
Medium Priority
?
81 Views
Last Modified: 2014-11-18
Hi,

Need code to delete left (old) users from our domain. I have list of the old users' usernames to run the script for it.

Do not need to delete home folders that sits on SAN or NAS or file servers as I have a different code to delete their shares and home folders.

So all I need is a code to delete their AD accounts in our domain.
Can you please help?

thanks
0
Comment
Question by:kuzum
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 29

Accepted Solution

by:
becraig earned 2000 total points
ID: 40341077
gc file.txt | % {Remove-ADUser -Identity $_ -whatif}

Open in new window

The above code has a whatif so you can validate the action, the command below is the actual run

gc file.txt | % {Remove-ADUser -Identity $_}

Open in new window


That will remove each user from your list from AD
0
 
LVL 29

Expert Comment

by:becraig
ID: 40341081
before running, be sure to run:
import-module ac*

This will load the active directory module so the command can run.
0
 
LVL 17

Expert Comment

by:Learnctx
ID: 40341108
Add -confirm:$false unless you want to have to confirm the deletions. By default the cmdlet will request confirmation unless you override it.

Remove-ADUser -Identity $_ -confirm:$false
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

762 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