Solved

How do I disable a user account using Powershell

Posted on 2009-04-04
2
3,624 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
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

911 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

17 Experts available now in Live!

Get 1:1 Help Now