Deleting Users


There are times when you need to delete user accounts, and with ADSI, you can very easily delete large numbers of users with the single click of a mouse. Some reasons for deleting user accounts follow:

  • To clean up a computer lab environment, that is, to return machines to a known state

  • To clean up accounts at the end of a school year. Many schools delete all student-related accounts and files at the end of each year. Scripting makes it easy to both create and delete the accounts.

  • To clean up temporary accounts created for special projects. If the creation of accounts is scripted, their deletion can also be scripted, ensuring no temporary accounts are left lingering in the directory.

image from book
Just the Steps

To delete users

  1. Perform the binding to the appropriate OU.

  2. Use [ADSI] to make a connection.

  3. Specify the appropriate provider and ADsPath.

  4. Call the Delete method.

  5. Specify object class as User.

  6. Specify the user to delete by CN.

image from book

To delete a user, call the Delete method after binding to the appropriate level in the Active Directory namespace. Then specify both the object class, which in this case is User, and the CN of the user to be deleted. This can actually be accomplished in only two lines of code:

 $objDomain = [ADSI]($provider + $ou + $domain) $objDomain.Delete $oClass, $oCn + $oUname

If you modify the image from book CreateUser.ps1 script, you can easily transform it into a image from book DeleteUser.ps1 script, which follows. The main change is in the Worker section of the script. The binding string is the same as shown earlier, and is shown here:

 $objADSI = [ADSI]"LDAP://ou=myTestOU,dc=nwtraders,dc=msft"

However, you use the connection that was made in the binding string, and call the Delete method. You specify the class of the object in the $strClass variable in the Reference section of the script. You also list the $strName. The syntax is Delete(Class, target). The deletion takes effect immediately. No SetInfo() command is required. This command is shown here:

 $objUser = $objADSI.delete($strCLass, $StrName)

The image from book DeleteUser.ps1 script entailed only two real changes from the image from book CreateUser.ps1 script. This makes user management very easy. If you need to create a large number of temporary users, you can save the script and then use it to get rid of them when they have completed their project. The complete image from book DeleteUser.ps1 script is shown here.

 DeleteUser.PS1 strCLass = "User" $StrName = "CN=MyNewUser" $objADSI = [ADSI]"LDAP://ou=myTestOU,dc=nwtraders,dc=msft" $objUser = $objADSI.delete($strCLass, $StrName)




Microsoft Press - Microsoft Windows PowerShell Step by Step
MicrosoftВ® Windows PowerShell(TM) Step By Step (Step By Step (Microsoft))
ISBN: 0735623953
EAN: 2147483647
Year: 2007
Pages: 128
Authors: Ed Wilson

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net