Skip to content

Instantly share code, notes, and snippets.

@iyre
Last active November 11, 2021 23:17
Show Gist options
  • Save iyre/2c0a5367675bd7c7e6c909fbb4ba7d4a to your computer and use it in GitHub Desktop.
Save iyre/2c0a5367675bd7c7e6c909fbb4ba7d4a to your computer and use it in GitHub Desktop.
update common organizational attributes in active directory using this powershell script
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)][ValidateSet('Title', 'Department', 'Company')]
[string]$AttributeName = (Read-host -Prompt "Which attribute? [Title, Department, Company]"),
[Parameter(Mandatory = $true)][string]$AttributeValueOld = (Read-Host -Prompt "Old value"),
[Parameter(Mandatory = $true)][string]$AttributeValueNew = (Read-Host -Prompt "New value"),
[switch]$NoConfirm
)
begin {}
process {
[array]$Users = Get-ADUser -Filter {$AttributeName -eq $AttributeValueOld}
if (-not $NoConfirm) {
Write-Host "'$AttributeName' attribute will be updated",
"`n'$AttributeValueOld' becomes '$AttributeValueNew'",
"`n$( $Users.Count ) users will be affected"
if ((Read-Host -Prompt "Continue? [Y/N]") -ne 'y') { return }
}
foreach ($User in $Users) {
switch ($AttributeName) {
'Title' { Set-ADUser -Identity $User -Title $AttributeValueNew }
'Department' { Set-ADUser -Identity $User -Department $AttributeValueNew }
'Company' { Set-ADUser -Identity $User -Company $AttributeValueNew }
}
Write-Verbose "Updated $( $User.Name )($( $User.sAMAccountName ))"
}
}
end {}
[CmdletBinding()]
param (
[string]$ManagerUsernameOld = (Read-host -Prompt "Old manager username"),
[string]$ManagerUsernameNew = (Read-Host -Prompt "New manager username")
)
begin {}
process {
try {
$ManagerOld = Get-ADUser -Identity $ManagerUsernameOld
$ManagerNew = Get-ADUser -Identity $ManagerUsernameNew
}
catch {
Write-Error "Failed to find accounts for the specified users." -ForegroundColor Red
return
}
$Users = Get-ADUser -Filter {Manager -eq $ManagerOld.DistinguishedName}
Write-Debug "Replacing '$($ManagerOld.Name)' with '$($ManagerNew.Name)'. This will affect $($Users.Count) users."
foreach ($User in $Users) {
Set-ADUser -Identity $User -Manager $ManagerNew.DistinguishedName
Write-Verbose "Updated $($User.Name)($($User.sAMAccountName))"
}
}
end {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment