Solved

How do I disable a user account using Powershell

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

688 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