Help with an Onboarding Script (find ad user, move object based on office location property)

Hi All,

I work for a company that get anywhere between 30-60 onboardings a month.
To make life easier over the past 6 months been trying to create a script which completes the following once the AD user object exists.

Inputting the users name displays their
DisplayName, sAMAccountName,Country,Company,Title,Office and then automatically move the account based on the listed office property.

understand ill need some sort of array or database where i can match the office property against but not entirely sure how to do this.


$title = "New User Set up
"

$title


$UserName = Read-Host -Prompt "Enter the Username "

Get-ADUser -Identity $UserName -Properties * | Select-Object DisplayName, sAMAccountName,Country,Company,Title,Office | FL

$OfficeLocation = Get-ADUser -Identity $UserName -Properties * | Select-Object Office 

the 1.0 version of this script i manually type in the the name of the location but with the entirety of emea under me it seems more reasonable to create the location ou then once the officelocation is picked up by the script match it in the array and move based on that.

$OUs = @{

Birmingham="OU=Birmingham ,OU=United Kingdom,OU=EMEA,OU=xxx - Users,DC=xxxx,DC=xxxx,DC=com";

London="OU=London ,OU=United Kingdom,OU=EMEA,OU=xxx - Users,DC=xxxx,DC=xxxx,DC=com";
 }

   $ShowOU = New-Object System.Management.Automation.Host.ChoiceDescription "&1" ,"Show list of available OUs"

   
   
   $options = [system.Management.Automation.host.choicedescription[]]($ShowOU)
   
   $result2 = $host.ui.PromptForChoice($title2, $message, $options, 0)
   
   switch ($result2) {
    0 { $OUs | Format-Table -AutoSize -Property Name }
    
   
}

Any help appreciated.

Hi, welcome to the forum :wave:

I would use a psd1 file as a configuration file. You can then maintain a configuration per office, which gives you flexibility if you want to add other configuration items per office in future, e.g. office specific distribution lists or groups.

Here’s a basic example using just the OU:

# UserConfig.psd1
@{
    Office = @{
        London     = @{
            OU = 'OU=London,OU=United Kingdom,OU=EMEA,OU=xxx - Users,DC=xxxx,DC=xxxx,DC=com'
        }
        Birmingham = @{
            OU = 'OU=London,OU=United Kingdom,OU=EMEA,OU=xxx - Users,DC=xxxx,DC=xxxx,DC=com'
        }
    }
}
$UserConfig = Import-PowerShellDataFile .\UserConfig.psd1

$UserName = Read-Host -Prompt "Enter the Username "

$User = Get-ADUser -Identity $UserName -Properties Office
$User | Move-ADObject -TargetPath $UserConfig.Office.$($User.Office).OU
2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.