Last active
September 14, 2024 20:49
-
-
Save stephanlamoureux/48eee09a7c33d2b71d81dbd2591b1548 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################### | |
### Run Script as Administrator ### | |
################################### | |
Import-Module ActiveDirectory | |
# Step 1: Check for the existence of the "Finance" Organizational Unit | |
try { | |
$ou = Get-ADOrganizationalUnit -Filter {Name -eq 'Finance'} -ErrorAction SilentlyContinue | |
if ($ou) { | |
Write-Host "Organizational Unit 'Finance' exists. Attempting to delete the OU." | |
try { | |
# Delete the "Finance" OU | |
Remove-ADOrganizationalUnit -Identity $ou.DistinguishedName -Recursive -Confirm:$false -ErrorAction Stop | |
Write-Host "Organizational Unit 'Finance' has been deleted." | |
} catch { | |
Write-Host "An error occurred while deleting the Finance OU: $_" | |
} | |
} else { | |
Write-Host "Organizational Unit 'Finance' does not exist." | |
} | |
} catch { | |
Write-Host "An error occurred while checking the Finance OU: $_" | |
} | |
# Step 2: Create the "Finance" OU | |
try { | |
New-ADOrganizationalUnit -Name "Finance" -Path "DC=consultingfirm,DC=com" -ErrorAction Stop | |
Write-Host "Organizational Unit 'Finance' has been created." | |
} catch { | |
Write-Host "An error occurred while creating the Finance OU: $_" | |
} | |
# Step 3: Import users from financePersonnel.csv into the "Finance" OU | |
$csvFilePath = "$PSScriptRoot\financePersonnel.csv" | |
if (Test-Path $csvFilePath) { | |
$users = Import-Csv $csvFilePath | |
foreach ($user in $users) { | |
$FirstName = $user.First_Name | |
$LastName = $user.Last_Name | |
$displayName = "$FirstName $LastName" | |
try { | |
New-ADUser -Name $displayName ` | |
-GivenName $FirstName ` | |
-Surname $LastName ` | |
-DisplayName $displayName ` | |
-PostalCode $user.PostalCode ` | |
-OfficePhone $user.OfficePhone ` | |
-MobilePhone $user.MobilePhone ` | |
-Path "OU=Finance,DC=consultingfirm,DC=com" ` | |
-Enabled $true ` | |
-AccountPassword (ConvertTo-SecureString "P@55word123" -AsPlainText -Force) ` | |
-ChangePasswordAtLogon $true -ErrorAction Stop | |
Write-Host "User $displayName has been added to the Finance OU." | |
} catch { | |
Write-Host "An error occurred while adding user ${displayName}: $_" | |
} | |
} | |
} else { | |
Write-Host "CSV file not found: $csvFilePath" | |
} | |
# Step 4: Generate output file for submission | |
try { | |
Get-ADUser -Filter * -SearchBase "OU=Finance,DC=consultingfirm,DC=com" -Properties DisplayName,PostalCode,OfficePhone,MobilePhone | | |
Select-Object DisplayName,PostalCode,OfficePhone,MobilePhone | | |
Out-File "$PSScriptRoot\AdResults.txt" | |
Write-Host "AD user export complete. Results saved to AdResults.txt." | |
} catch { | |
Write-Host "An error occurred while exporting AD user data: $_" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment