?
Solved

How do I disable a user account using Powershell

Posted on 2009-04-04
2
Medium Priority
?
3,651 Views
Last Modified: 2013-12-24
I have been asked to develop a Powershell script that will do the following:
1) Disable a user account
2) Reset the user password to a random 6 character alpha and numeric password
3) Hide from the Global Address List
4) Restrict from sending/receiving email

Ideally we would like to be prompted for the user ID.  Also, would like to be able to import from a file for a large number of users, but that won't always be the case.
0
Comment
Question by:sanderson321
[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 Comments
 
LVL 11

Accepted Solution

by:
Zuhir Elgmati earned 2000 total points
ID: 24071459
----------------------------------------------------------------disable or enable user accounts --------------

function get-dn ($SAMName)
{
       $root = [ADSI]''
       $searcher = new-object     System.DirectoryServices.DirectorySearcher($root)
      $searcher.filter = "(&(objectClass=user)(sAMAccountName= $SAMName))"
      $user = $searcher.findall()

      if ($user.count -gt 1)
      {    
            $count = 0
            foreach($i in $user)
            {
                  write-host $count ": " $i.path
                  $count = $count + 1
            }

            $selection = Read-Host "Please select item: "

return $user[$selection].path

      }
      else
      {
              return $user[0].path
      }
}

$Name = $args[0]
$status = $args[1]
$path = get-dn $Name
"'" + $path + "'"  

if ($status -match "enable")
      {
            # Enable the account
            $account=[ADSI]$path
            $account.psbase.invokeset("AccountDisabled", "False")
            $account.setinfo()
      }
else
      {
            # Disable the account
            $account=[ADSI]$path
            $account.psbase.invokeset("AccountDisabled", "True")
            $account.setinfo()
      }
------------------------------------------------------------------------------
Hiding Contacts from Global Address List using Exchange Powershell
http://it.peterspowerblog.com/2008/07/03/hiding-contacts-from-global-address-list-using-exchange-powershell.aspx

------------------------------------------------------------------------------
Reset Password
http://liveatedu.spaces.live.com/Blog/cns!C76EAE4D4A509FBD!430.entry

---------------------------------------------------------------------------------
0
 
LVL 18

Expert Comment

by:BSonPosh
ID: 24076992
Or you could use the free cmdlets from Quest: www.quest.com/powershell

They make this a trivial task.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Here's a look at newsworthy articles and community happenings during the last month.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

770 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