Follow this blog:
RSS
Email Alert

TR Dojo

Video: Three PowerShell scripts for managing users in Active Directory Domain Services

Takeaway: Bill Detwiler shares three scripts that make it easy to manage users in Active Directory via PowerShell, such as copying group memberships.

July 27, 2010, 7:53 AM PDT | Length:00:05:03

View Transcript

Obtaining user object information via Active Directory Users And Computers is fine for the one-time use, but it falls short for batch tasks. During this episode of TR Dojo, I’ll share three scripts make it easy to pull user information from Active Directory via PowerShell.

Why aren't you using PowerShell to manage your Active Directory environment?

For those who prefer text to video, you can click the Transcript link that appears below the video player window. And to make it easier for you to try out and modify the PowerShell scripts I discuss in the video, here is the text for each one:

Changing an attribute for a group of users

Get-ADUser -Filter * -SearchBase "OU=Accounting,OU=UserAccounts,DC=YourDomain,DC=com" | Set-ADUser -Manager "John Doe"

Copying one user’s group memberships to a second user

Get-ADPrincipalGroupMembership -Identity JohnDoe | % {Add-ADPrincipalGroupMembership -Identity JaneDoe -MemberOf $_}

Pulling a list of user accounts with passwords that don’t expire

Search-ADAccount -PasswordNeverExpires | FT Name, ObjectClass, UserPrincipalName

For more PowerShell tips and tricks, check out the following resources from TechRepublic and around the Web:

You can also sign up to receive the latest TR Dojo lessons through one or more of the following methods:

Get IT Tips, news, and reviews delivered directly to your inbox by subscribing to TechRepublic’s free newsletters.

Bill Detwiler

About Bill Detwiler

Bill Detwiler is Head Technology Editor of TechRepublic. Previously, he worked as a Support Tech and IT Manager in the social research and energy industries.

Bill Detwiler

Bill Detwiler
Bill Detwiler is Head Technology Editor for TechRepublic. Previously he worked as a Technical Support Associate and Information Technology Manager in the social research and energy industries. Bill is a Microsoft Certified Professional with experience in Windows administration, data management, desktop support, and system security.

Bill Detwiler

Bill Detwiler
Bill Detwiler has nothing to disclose. He doesn't hold investments in the technology companies he covers.

Transcript

Bill Detwiler: Obtaining user object information via Active Directory Users And Computers is fine for the one-time use, but it falls short for batch tasks.

 

I'm Bill Detwiler, and during this episode of TR Dojo, I'll share three scripts make it easy to pull user information from Active Directory via PowerShell.

 

In a, earlier TR Dojo episode, I showed you how to retrieve a list of installed USB devices and kill processes using PowerShell.

 

But with the release of the Active Directory Module for Windows PowerShell, an enhancement in Windows Server 2008 R2, this utility has become an even more useful tool for network administrators.

 

This PowerShell environment has several commands optimized for Active Directory and includes features not available through the normal Active Directory Users and Computers GUI.

 

And while there are plenty of command-line tools for flat dumps and exports, the new AD Module for Windows PowerShell is one of the most powerful tools for managing users in Active Directory Domain Services.

 

Let's start with changing an attribute for a group of users -- a task easily performed with the Get-ADUser and Set-ADUser cmdlets.

 

Suppose a new manager has taken over an Accounting department and you want to reflect that change in Active Directory.

 

From within an Active Directory Module for Windows PowerShell window, you could enter something like the command show here:

 

Get-ADUser -Filter * -SearchBase "OU=Accounting,OU=UserAccounts,DC=YourDomain,DC=com" | Set-ADUser -Manager "John Doe"

 

This one-line PowerShell script uses the Get-ADUser cmdlet with the -Filter and -SearchBase parameters to return all user accounts with the Accounting OU and then sends the output to the Set-ADUser cmdlet, which then sets the manager attribute to the value John Doe.

 

And don't worry, I'll include the text for each script I cover during this show in the blog notes.

 

Now, looking at the user accounts in the Accounting department through the AD Users and Computers GUI, we can see that the manager attribute has been set to John Doe.

 

For our second task, suppose an existing employee has transferred into a new department, and needs the same group memberships as others in that department. Using the Get-ADPrincialGroupMembership and Add-ADPrincipalGroupMembership cmdlets you can do just that.

 

Again from within the Active Directory Module for Windows PowerShell window, you would enter something like this script:

 

Get-ADPrincipalGroupMembership -Identity JohnDoe | % {Add-ADPrincipalGroupMembership -Identity JaneDoe -MemberOf $_}

 

This script gets the group memberships of user John Doe and transfers them to user Jane Doe.

 

Looking at the accounts with the AD Users and Computers GUI, we see that Jane Doe now has the same group memberships as John Doe.

 

Our last script comes from TechRepublic blogger Rick Vanover. In a recent article, he explained how the Search-ADAccount cmdlet to pull a list of all user accounts with non-expiring passwords. Having this information can be really helpful during a security audit.

 

To execute the script, open the Windows PowerShell window and enter the script shown here:

 

Search-ADAccount -PasswordNeverExpires | FT Name, ObjectClass, UserPrincipalName

 

When you run the script, you should see a table showing the user name, object class, and user principal name of each account with a non-expiring password.

 

I've shown you just the tip of the iceberg when it comes to working with Active Directory through PowerShell. For more tips, I encourage you to check out this episode's blog notes, where I'll link to more PowerShell resources from TechRepublic and beyond.

And as always, for more teachings on your path to becoming an IT Ninja, visit trdojo.techrepublic.com, or you can follow me on Twitter at twitter.com/billdetwiler.

 

Thanks for visiting the TR Dojo.

10
Comments

Join the conversation!

Follow via:
RSS
Email Alert