From d48a83204ce03d8d3080a4e86fbbc565c7a042bc Mon Sep 17 00:00:00 2001 From: reiyua Date: Sat, 13 Apr 2024 00:28:14 +1000 Subject: [PATCH] Working code --- AutoUserADCreator.ps1 | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/AutoUserADCreator.ps1 b/AutoUserADCreator.ps1 index abe4418..6378996 100644 --- a/AutoUserADCreator.ps1 +++ b/AutoUserADCreator.ps1 @@ -39,13 +39,13 @@ ForEach ($user in $fusers) { # Check if user already exists within OU. Skip if so with message stating so. (RAYYAN Contribution) # Source for code (https://morgantechspace.com/2016/11/check-if-ad-user-exists-with-powershell.html) - if (Get-ADUser -Filter "SamAccountName -eq '$fname$lname'") { - Write-Host "User $fname$lname already exists. Skipping." + if (Get-ADUser -Filter {SamAccountName -eq "$($fname + $lname)"}) { + Write-Host "User $($fname + $lname) already exists. Skipping." } else { # Construct the full name for user $fullName = "$fname $lname" # Create the user with full name now defined - new-ADUser -Name $fullName -SamAccountName ($fname + $lname) -UserPrincipalName "$fname@PSTest.local" -Path $OUpath -AccountPassword $fsecPass -Enabled $true -PassThru + new-ADUser -Name "$fname $lname" -SamAccountName ($fname + $lname) -UserPrincipalName "$fname@PSTest.local" -Path $OUpath -AccountPassword $fsecPass -Enabled $true -PassThru } } @@ -54,5 +54,3 @@ ForEach ($user in $fusers) { # Source for code (https://www.thomasmaurer.ch/2021/01/how-to-add-sleep-wait-pause-in-a-powershell-script/#:~:text=Read%2DHost%20%2DPrompt%20%22Press%20any%20key%20to%20continue...%22) Read-Host -Prompt "User creation completed, press any key to close the window." - -`