Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

POWERSHELL ACTIVE DIRECTORY: ADD OR UPDATE (CHANGE) MANAGER NAME IN ORGANIZATION TAB OF USER

I am sharing few of the scripts I wrote long back to change/update Users information in Active directory,  It was required me while migrating Active directory to Office 365. One of the script was adding or change manager name in organization tab of User properties. (All user names used here are fiction and not related to real world).

Active directory users and computers ad user properties organization add change or update manager name

POWERSHELL ACTIVE DIRECTORY: ADD OR UPDATE PROXYADDRESSES IN USER PROPERTIES ATTRIBUTE EDITOR

Before I start, I have created CSV file with user and manger information. Sample of the content in file are as below. Make sure you don't have empty or null value in either Name or Manager column. CSV file can be edited in Excel file, While saving make sure extension of the file is .csv.

NameManager                                                                 Active directory users and computers ad user properties organization add change or update manager name csv file format UserName and Mager change add
Adam.BaumX.Benedict
Adam.ZapelAthol
Art.MajorBud.Wieser
AtholC.Good
B.A.WareBud.Wieser
Barb.DwyerBud.Wieser

Download this code from Github. Before running make sure you have appropriate access to change ADUser properties, Copy this code and paste in notepad, extension should be .ps1. One of the requirement is Powershell ActiveDirectory module should be installed, By default it is installed on Domain Controller while AD installation, If you are running this script from your desktop download and install RSAT (Remote server administration tools https://www.microsoft.com/en-in/download/details.aspx?id=45520) from Microsoft site for your OS build and version, I am directly running scripts on Windows 2012 R2 domain controller. 

Another requirement is make sure you can execute script by running Set-ExecutionPolicy Unrestricted -Force (For production I use RemoteSigned policy, or change it to restricted back once I done with my work, This change will not happen without opening Powershell as an Administrator)

Powershell ps1 script set-executionpolicy unrestricted ad user update change

In my example my PS1 and CSV file is in temp file, Run command c:\temp\Update-AdUserManager.ps1 -File C:\temp\users.csv. Once it is executed successfully, it opens result in notepad, where I can review changes, same can be seen as below screenshot, also verify the same in user properties. If it is not able to find any user or manager from csv  in the AD it throws error on console.

Update-ADUserManager change manager active directory user properties csv, powershell

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
##############################
#.SYNOPSIS
#Add or update user's manager in Active Directory.
#
#.DESCRIPTION
#The Update-ADUserManager cmdlet add or update users properties (Manager Name under Orgnaization Tab) from CSV file, Once properties updated successfully
#
#.PARAMETER File
#This is a File path of CSV with name (samaccountname) of user and his manager name.Below is the CSV file format example, Make sure you don't have empty or null values in user's or manger's cell.
#Name	    Manager
#---------- ------------
#Adam.Baum	Bud.Wieser
#Adam.Zapel	Bud.Wieser
#Art.Major	Adam.Baum
#
#.EXAMPLE
#Update-ADUserManager -File C:\temp\users.csv
#
#.NOTES
#http://vcloud-lab.com
#Written using powershell version 5
#Script code version 1.0
###############################
[CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='Medium')]
param(
	[Parameter(
        Position=0, 
        Mandatory=$true,
        ValueFromPipeline=$true,
        HelpMessage='Type the full path of CSV file'
    )]
    [alias('Path', 'CSV')]
    [ValidateScript({
        If (Test-Path $_) {
            $true
        }
        else{
            "Invalid path given: $_"
        }
    })]
    [System.String]$File
)  
Begin {
    If (!(Get-Module ActiveDirectory)) {
        Import-Module ActiveDirectory
    }
    $username = Import-Csv -Path $File
    $Report = @()
}
Process {
    foreach ($user in $username) {
        $SamAccountName = $user.Name 
        Try {
            $GADuser = Get-ADUser -Filter {SamAccountName -eq $SamAccountName} -ErrorAction Stop
            $GADuser | Set-ADuser -Manager $user.Manager -ErrorAction Stop
            $Report += Get-ADUser -Filter {SamAccountName -eq $SamAccountName}  -Properties Manager | select Name, @{N='Manager';E={(Get-ADUser $_.Manager).Name}}
            Write-Verbose -Message "Record updated for $SamAccountName"
        }
        catch {
            Write-Error -Message "$SamAccountName or its manager does not exist please check in Active Directory"
        }
    }
}
End {
    $temp = [System.IO.Path]::GetTempFileName()
    $report | Out-file -FilePath $temp
    Write-Verbose -Message 'Opening report'
    Notepad $temp
    #c:\temp\users.csv
}

Useful articles
POWERSHELL: INSTALLING AND CONFIGURING ACTIVE DIRECTORY 
POWERSHELL ACTIVE DIRECTORY: ADD OR UPDATE (CHANGE) MANAGER NAME IN ORGANIZATION TAB OF USER
POWERSHELL ACTIVE DIRECTORY: ADD OR UPDATE PROXYADDRESSES IN USER PROPERTIES ATTRIBUTE EDITOR
Powershell one liner: Create multiple user accounts
Active Directory Powershell: Create bulk users from CSV file

Go Back



Comment

Blog Search

Page Views

11272294

Follow me on Blogarama