Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I disable a user account using Powershell

Posted on 2009-04-04
2
Medium Priority
?
3,658 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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

In this article, we’ll look at how to deploy ProxySQL.
How to deal with a specific error when using the Enable-RemoteMailbox cmdlet to create a mailbox in the cloud-based service, for an existing user in an on-premises Active Directory.
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 from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
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…
Suggested Courses

598 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