Solved

How do I disable a user account using Powershell

Posted on 2009-04-04
2
3,617 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
2 Comments
 
LVL 11

Accepted Solution

by:
Zuhir Elgmati earned 500 total points
Comment Utility
----------------------------------------------------------------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
Comment Utility
Or you could use the free cmdlets from Quest: www.quest.com/powershell

They make this a trivial task.
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

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

7 Experts available now in Live!

Get 1:1 Help Now