Active Directory Command Line Commands

7 min read Oct 10, 2024
Active Directory Command Line Commands

Mastering Active Directory with Command Line Commands

Active Directory (AD) is the cornerstone of many Windows networks, providing centralized management for users, computers, and resources. While the graphical user interface (GUI) in Active Directory Users and Computers (ADUC) is user-friendly, leveraging command-line tools can be incredibly powerful and efficient, particularly for automation and scripting.

Why Use Command Line Tools?

  • Speed and Efficiency: Command lines can execute tasks faster than navigating through menus.
  • Automation: Batch scripts can automate repetitive tasks, saving time and reducing errors.
  • Remote Management: Manage AD from a different computer.
  • Scripting and Integration: Integrate AD management into larger scripts and systems.

Getting Started: Understanding the Tools

Two primary tools are used for managing Active Directory from the command line:

  • DSADD: This tool adds objects to Active Directory.
  • DSMOD: This tool modifies existing objects in Active Directory.

Essential Command Line Commands for AD Management:

1. User Management

  • Adding a User:
    dsadd user "OU=Users,DC=example,DC=com" -samid "newuser" -displayname "New User" -password "password"
    
    • Replace "OU=Users,DC=example,DC=com" with the location of the user object.
    • "newuser" is the username.
    • "New User" is the display name.
    • "password" is the user's initial password.
  • Modifying a User:
    dsmod user "CN=newuser,OU=Users,DC=example,DC=com" -displayname "Updated Name" -password "newpassword"
    
    • "CN=newuser,OU=Users,DC=example,DC=com" identifies the user.
    • "Updated Name" sets a new display name.
    • "newpassword" changes the password.
  • Deleting a User:
    dsrm user "CN=newuser,OU=Users,DC=example,DC=com"
    
    • "CN=newuser,OU=Users,DC=example,DC=com" specifies the user to be deleted.

2. Group Management

  • Adding a Group:
    dsadd group "OU=Groups,DC=example,DC=com" -samid "newgroup" -displayname "New Group"
    
    • "OU=Groups,DC=example,DC=com" defines the group's location.
    • "newgroup" is the group name.
    • "New Group" is the display name.
  • Adding Members to a Group:
    dsadd group "CN=newgroup,OU=Groups,DC=example,DC=com" -members "CN=newuser,OU=Users,DC=example,DC=com"
    
    • "CN=newgroup,OU=Groups,DC=example,DC=com" identifies the group.
    • "CN=newuser,OU=Users,DC=example,DC=com" adds a user to the group.
  • Removing Members from a Group:
    dsmod group "CN=newgroup,OU=Groups,DC=example,DC=com" -removemember "CN=newuser,OU=Users,DC=example,DC=com"
    
    • "CN=newgroup,OU=Groups,DC=example,DC=com" defines the group.
    • "CN=newuser,OU=Users,DC=example,DC=com" removes a user from the group.

3. Computer Management

  • Adding a Computer:
    dsadd computer "OU=Computers,DC=example,DC=com" -samid "newcomputer"
    
    • "OU=Computers,DC=example,DC=com" indicates the computer's location.
    • "newcomputer" is the computer name.
  • Changing a Computer's OU:
    dsmod computer "CN=newcomputer,OU=Computers,DC=example,DC=com" -newname "OU=Workstations,DC=example,DC=com"
    
    • "CN=newcomputer,OU=Computers,DC=example,DC=com" identifies the computer.
    • "OU=Workstations,DC=example,DC=com" specifies the new OU.
  • Deleting a Computer:
    dsrm computer "CN=newcomputer,OU=Computers,DC=example,DC=com"
    
    • "CN=newcomputer,OU=Computers,DC=example,DC=com" specifies the computer to be deleted.

4. Other Important Commands

  • Getting Object Information:
    dsquery user -name "newuser" 
    
    • This command retrieves details about a user.
    • You can use dsquery to find computers, groups, and other objects.
  • Searching Active Directory:
    dsquery * -samid "*" -filter "(&(objectClass=user)(objectCategory=person)(displayName=*Smith*))"
    
    • This command searches for users with "Smith" in their display name.
  • Modifying Object Attributes:
    dsmod user "CN=newuser,OU=Users,DC=example,DC=com" -replace  description "New description"
    
    • This command updates the "description" attribute for a user.

Best Practices and Tips

  • Understanding Distinguished Names (DNs): DNs like "CN=newuser,OU=Users,DC=example,DC=com" uniquely identify objects within AD.
  • Using the -server Parameter: Specify the domain controller you want to interact with.
  • Testing and Validation: Run commands in a test environment before implementing them in production.
  • Error Handling: Use dsquery to verify object names and attributes before attempting modifications.
  • Documentation: Document your scripts and commands for future reference.

Advanced Options and Resources:

  • PowerShell: PowerShell is an excellent scripting language that integrates seamlessly with Active Directory, providing a more robust and flexible approach.
  • Active Directory Module for PowerShell: This module provides cmdlets for advanced AD management tasks.
  • Microsoft Documentation: Refer to the official Microsoft documentation for comprehensive details on Active Directory command-line tools.

Conclusion

Mastering Active Directory command-line commands empowers you to manage your network effectively and efficiently. Whether you're automating repetitive tasks, scripting complex operations, or simply seeking to streamline your AD management, command-line tools provide a powerful and versatile approach. By understanding the basics and adhering to best practices, you can harness the full potential of Active Directory for robust and secure network management.