Managing Active Directory users, groups, and computers with PowerShell – PowerShell and Active Directory – Learning PowerShell

PowerShell provides powerful cmdlets for managing Active Directory users, groups, and computers. Here are some common tasks you can perform with PowerShell to manage Active Directory:

  1. Connect to Active Directory: Before you can manage Active Directory with PowerShell, you need to establish a connection. Use the Import-Module cmdlet to import the Active Directory module, and then use the Get-Credential cmdlet to enter your Active Directory credentials. Finally, use the Connect-AzureAD cmdlet to connect to your Active Directory environment.
Import-Module ActiveDirectory
$credential = Get-Credential
Connect-AzureAD -Credential $credential
  1. Managing users:
  • Creating a new user: Use the New-ADUser cmdlet to create a new user in Active Directory, specifying the necessary parameters such as Name, SamAccountName, GivenName, Surname, and UserPrincipalName.
   New-ADUser -Name "John Doe" -SamAccountName "jdoe" -GivenName "John" -Surname "Doe" -UserPrincipalName "[email protected]"
  • Modifying user attributes: Use the Set-ADUser cmdlet to modify attributes of an existing user in Active Directory.
   Set-ADUser -Identity "jdoe" -Department "Sales"
  • Disabling a user: Use the Disable-ADAccount cmdlet to disable a user account in Active Directory.
   Disable-ADAccount -Identity "jdoe"
  • Resetting a user’s password: Use the Set-ADAccountPassword cmdlet to reset a user’s password.
   Set-ADAccountPassword -Identity "jdoe" -Reset
  1. Managing groups:
  • Creating a new group: Use the New-ADGroup cmdlet to create a new group in Active Directory, specifying the necessary parameters such as Name and SamAccountName.
   New-ADGroup -Name "Marketing Group" -SamAccountName "MarketingGroup"
  • Adding members to a group: Use the Add-ADGroupMember cmdlet to add members to an existing group in Active Directory.
   Add-ADGroupMember -Identity "MarketingGroup" -Members "jdoe", "asmith"
  • Removing members from a group: Use the Remove-ADGroupMember cmdlet to remove members from an existing group in Active Directory.
   Remove-AdGroupMember -Identity "MarketingGroup" -Members "asmith"
  1. Managing computers:
  • Creating a new computer object: Use the New-ADComputer cmdlet to create a new computer object in Active Directory, specifying the necessary parameters such as Name and SamAccountName.
   New-ADComputer -Name "Workstation1" -SamAccountName "WS-1"
  • Moving a computer object: Use the Move-ADObject cmdlet to move a computer object to a different organizational unit (OU) in Active Directory.
   Move-ADObject -Identity "CN=Workstation1,OU=Computers,DC=example,DC=com" -TargetPath "OU=NewOU,DC=example,DC=com"
  • Disabling a computer object: Use the Disable-ADAccount cmdlet to disable a computer object in Active Directory.
   Disable-ADAccount -Identity "WS-1"

PowerShell provides powerful cmdlets for managing Active Directory (AD) users, groups, and computers. Here’s an overview of how you can use PowerShell to perform common tasks in AD management:

Connecting to Active Directory:
Before you can manage AD using PowerShell, you need to establish a connection to the AD domain. Use the Import-Module ActiveDirectory cmdlet to import the Active Directory module, and then use the Connect-ADServiceAccount or Get-Credential cmdlets to provide the necessary credentials for connecting to AD.

Managing Users:
PowerShell allows you to create, modify, and delete AD user accounts. Here are some useful cmdlets for managing users:

New-ADUser: Creates a new user account in AD. For example:

New-ADUser -Name “John Doe” -SamAccountName “johndoe” -GivenName “John” -Surname “Doe”

Set-ADUser: Modifies properties of an existing user account. For example, to change a user's department:

Set-ADUser -Identity "johndoe" -Department "Sales"

Get-ADUser: Retrieves information about AD user accounts. For example, to get all users in a specific department:
Get-ADUser -Filter {Department -eq “Sales”}

Remove-ADUser: Deletes an AD user account. For example:

Remove-ADUser -Identity "johndoe"

Managing Groups:
PowerShell enables you to create, modify, and remove AD groups. Here are some common cmdlets for group management:

New-ADGroup: Creates a new AD group. For example:

New-ADGroup -Name “IT Support” -SamAccountName “ITSupport” -GroupCategory Security -GroupScope Global

Add-ADGroupMember: Adds members to an AD group. For example, to add a user to a group:

Add-ADGroupMember -Identity "ITSupport" -Members "johndoe"

Get-ADGroup: Retrieves information about AD groups. For example, to get all groups in a specific organizational unit (OU):

Get-ADGroup -Filter * -SearchBase “OU=Groups,DC=domain,DC=com”

Remove-ADGroup: Deletes an AD group. For example:

Remove-ADGroup -Identity "ITSupport"

Managing Computers:
PowerShell allows you to manage AD computer accounts as well. Here are some useful cmdlets for computer management:

New-ADComputer: Creates a new computer account in AD. For example:
powershell
New-ADComputer -Name “Computer01” -SamAccountName “Computer01” -Enabled $true

Move-ADObject: Moves a computer object to a different OU. For example:

Move-ADObject -Identity "CN=Computer01,OU=Computers,DC=domain,DC=com" -TargetPath "OU=NewOU,DC=domain,DC=com"

Get-ADComputer: Retrieves information about AD computer accounts. For example, to get all computers in a specific OU:
powershell
Get-ADComputer -Filter * -SearchBase “OU=Computers,DC=domain,DC=com”

Remove-ADComputer: Deletes an AD computer account. For example:

Remove-ADComputer -Identity "Computer01"

These are just a few examples of how you can manage AD users, groups, and computers using PowerShell. PowerShell’s Active Directory module provides a wide range of cmdlets that allow you to automate and streamline administrative tasks in an AD environment.

These are just a few examples of what you can accomplish with PowerShell when managing Active Directory. PowerShell provides a vast array of cmdlets and functionalities that allow for extensive automation and customization of Active Directory management tasks.

SHARE
By Shanley

Leave a Reply

Your email address will not be published. Required fields are marked *

No widgets found. Go to Widget page and add the widget in Offcanvas Sidebar Widget Area.