We help IT Professionals succeed at work.

How to jump to the next an Active Directory User within a ForEach statement ?

706 Views
Last Modified: 2013-01-03
Hello,

I have a foreach statement that queries/reads AD users and prompts some questions while querying the AD. If there is a question that answers No, it will have to jump to the next user.

code:
$ad = Get-ADUser -filter * -Properties sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber  -SearchBase "OU=ou,DC=domain,DC=com" | Select-Object  sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber

foreach ($AD_User in $ad) {
      if ($file[0].sn -eq $AD_User.sn)
      {
            #Write-Host "BINGO FOUND IT"
               #Ask Questions
              $RightWrong = Read-Host 'NOTE: Is this the right Employee, You are working on? (Y/N)'

#I AM STUCK HERE...
if $RightWrong -eq "N" {

     #It needs to jump to the next AD user and Ask questions
}
}

Thanks for your help,
Comment
Watch Question

Subash SundharanIT Infrastructure Architect
CERTIFIED EXPERT

Commented:
Try..
if ($RightWrong -eq "N") {break}

Open in new window

namergSystems Administrator

Author

Commented:
Hey Guru Subsun,

But will it not break the "for" statement ?
Subash SundharanIT Infrastructure Architect
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
namergSystems Administrator

Author

Commented:
hmm, it did not move to the next AD user, it kept the same value or AD User found on the if statement.  
if ($file[0].sn -eq $AD_User.sn) {....}
namergSystems Administrator

Author

Commented:
By the way this is all related to my previous questions, it is like a mini application but with poweshell.
Subash SundharanIT Infrastructure Architect
CERTIFIED EXPERT

Commented:
I think you have to place the second IF statement outside the first one.. Try this..
foreach ($AD_User in $ad) {
      if ($file[0].sn -eq $AD_User.sn)
      {
      #Write-Host "BINGO FOUND IT"
      #Ask Questions
      $RightWrong = Read-Host 'NOTE: Is this the right Employee, You are working on? (Y/N)'
      }
      if ($RightWrong -eq "N") {[void]$foreach.moveNext()}
}

Open in new window

namergSystems Administrator

Author

Commented:
Same thing,

Result:
======== FOUND THE FOLLOWING EMPLOYEE IN ACTIVE DIRECTORY ========
LASTNAME: Silveira
FIRST NAME: Rodrigo
DEPARTMENT: Information Services
TITLE: Sr System/NetworkAdministrator
TELEPHONE NUMBER: 303-333-3333
EMPLOYEE ID:
EMPLOYEE NUMBER:
=================================================

NOTE: Is this the right Employee, You are working on? (Y/N): n

#The following comes after the following code:
#if ($RightWrong -eq "N") {
#                  #break
#                  [void]$foreach.moveNext()
#                  Write-Host "LASTNAME: $($AD_USER.sn)"
#                  Write-Host "FIRST NAME: $($AD_USER.givenName)"
                  
            }
#Same user as == FOUND THE FOLLOWING EMPLOYEE IN ACTIVE DIRECTORY ======== section

LASTNAME: Silveira
FIRST NAME: Rodrigo

NOTE: Is the EMPLOYEE Information FOUND In ACTIVE DIRECTORY CORRECT AGAINST The EMPLOYEE Information FOUND In the File? (Y/N): PS C:\scripts\AD>
Subash SundharanIT Infrastructure Architect
CERTIFIED EXPERT

Commented:
BTB there will be only one user which match condition $file[0].sn -eq $AD_User.sn right? It yes.. Ideally the script should exit if you input "N". Please correct me if I am wrong in my assumption..
namergSystems Administrator

Author

Commented:
Correct. The result of that of IF is, 1, True.
But think about that can be more than one User with a lastname Silveira.
Subash SundharanIT Infrastructure Architect
CERTIFIED EXPERT

Commented:
[void]$foreach.moveNext()
                 Write-Host "LASTNAME: $($AD_USER.sn)"
                  Write-Host "FIRST NAME: $($AD_USER.givenName)"

Write-Host here will not write the next user, if will just write the same user which is in pipeline..

It would be better if you can post the complete code..
namergSystems Administrator

Author

Commented:
Here it goes:

*******************************************************

Clear-Host
$file  = Import-Csv "c:\scripts\ad\temp\file.csv" -header sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
$ad = Get-ADUser -filter * -Properties sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber  -SearchBase "OU=OU,DC=colo,DC=pvt" | Select-Object  sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
Write-Host "Querying   Employee from   file against Active Directory"
Write-Host ""
Write-Host "++ FOUND THE FOLLOWING EMPLOYEE IN   FILE +++++"
#Write-Host ""
Write-Host "LASTNAME: $($ [0].sn)"
Write-Host "FIRST NAME: $($ [0].givenName)"
Write-Host "DEPARTMENT: $($ [0].department)"
Write-Host "TITLE: $($ [0].title)"
Write-Host "TELEPHONE NUMBER: $($ [0].telephoneNumber)"
Write-Host "FLEX ID: $($ [0].employeeID)"
Write-Host "CLOCK NUMBER: $($ [0].employeeNumber)"
#Write-Host ""
Write-Host "++++++++++++++++++++++++++++++++++++++++++++++++++++++"
#Write-Host "Waiting for 5 seconds"
Start-Sleep -Seconds 5
foreach ($AD_User in $ad) {
      if ($file[0].sn -eq $AD_User.sn)
      {
            #Write-Host "BINGO FOUND IT"
            $Flag LastName = 1
            Write-Host ""
            Write-Host "==== FOUND THE FOLLOWING EMPLOYEE IN ACTIVE DIRECTORY =="
            Write-Host "LASTNAME: $($AD_USER.sn)"
            Write-Host "FIRST NAME: $($AD_USER.givenName)"
            Write-Host "DEPARTMENT: $($AD_USER.department)"
            Write-Host "TITLE: $($AD_USER.title)"
            Write-Host "TELEPHONE NUMBER: $($AD_USER.telephoneNumber)"
            Write-Host "EMPLOYEE ID: $($AD_USER.employeeID)"
            Write-Host "EMPLOYEE NUMBER: $($AD_USER.employeeNumber)"
            Write-Host "================================================================"
            Write-Host ""
            $RightWrong = Read-Host 'NOTE: Is this the right   Employee, You are working on? (Y/N)'
            Write-Host ""
            if ($RightWrong -eq "N") {
            #I AM STUCK HERE
                  #break
                  [void]$foreach.moveNext()
                  Write-Host "LASTNAME: $($AD_USER.sn)"
                  Write-Host "FIRST NAME: $($AD_USER.givenName)"
                  
            }
            $YesNo = Read-Host 'NOTE: Is the EMPLOYEE Information FOUND In ACTIVE DIRECTORY CORRECT AGAINST The EMPLOYEE Information FOUND In the   File? (Y/N)'
            Write-Host ""
            #Write-Host "YesNo Value: $($YesNo)"
            if (($RightWrong -eq "Y") -and ($YesNo -eq "N"))
               {
                        Write-Host "Which of the following Employee information would you like to change in Active Directory ?"
                        Write-Host ""
                        $AD_User_Change = Read-Host '1 = LASTNAME. 2 = FIRST NAME. 3 = DEPARTMENT. 4 = TITLE. 5 = TELEPHONE NUMBER. 6 = EMPLOYEE ID. 7 = EMPLOYEE NUMBER'
                  }
            switch ($AD_User_Change) {
             1 {
                  Write-Host "1"
                  $InputLastName = Read-Host 'Type the new Last Name:'
                  Write-Host ""
                  $NewLastName = Read-Host "Are you sure, you want to replace the following lastname ""$($AD_User.sn)"" by ""$InputLastName"" (Y/N)"
                  if (($NewLastName -eq "y"))
                     {
                        Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -surname $InputLastName -DisplayName $($InputLastName+", "+$AD_USER.givenName) -ErrorAction SilentlyContinue -ErrorVariable Err1
                        Start-Sleep -Seconds 5
                        $NewName = $InputLastName+", "+$AD_USER.givenName
                        Get-ADUser $AD_User.sAMAccountName  | Rename-ADObject -newname $NewName
                        Start-Sleep -Seconds 5
                        Write-Host "New LastName SET"
                        Start-Sleep -Seconds 30
                    }
              }
             2 {Write-Host "2"}
             3 {Write-Host "3"}
             4 {Write-Host "4"}
             5 {Write-Host "5"}
             6 {Write-Host "6"}
             7 {Write-Host "7"}
            }
            Start-Sleep -Seconds 30
      }

}
Subash SundharanIT Infrastructure Architect
CERTIFIED EXPERT

Commented:
Can you try this and see if it gives expected result?
Clear-Host
$file  = Import-Csv "c:\scripts\ad\temp\file.csv" -header sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
$ad = Get-ADUser -filter * -Properties sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber  -SearchBase "OU=OU,DC=colo,DC=pvt" | Select-Object  sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
Write-Host "Querying   Employee from   file against Active Directory"
Write-Host ""
Write-Host "++ FOUND THE FOLLOWING EMPLOYEE IN   FILE +++++"
#Write-Host ""
Write-Host "LASTNAME: $($ [0].sn)"
Write-Host "FIRST NAME: $($ [0].givenName)"
Write-Host "DEPARTMENT: $($ [0].department)"
Write-Host "TITLE: $($ [0].title)"
Write-Host "TELEPHONE NUMBER: $($ [0].telephoneNumber)"
Write-Host "FLEX ID: $($ [0].employeeID)"
Write-Host "CLOCK NUMBER: $($ [0].employeeNumber)"
#Write-Host ""
Write-Host "++++++++++++++++++++++++++++++++++++++++++++++++++++++"
#Write-Host "Waiting for 5 seconds"
Start-Sleep -Seconds 5
foreach ($AD_User in $ad) {
      if ($file[0].sn -eq $AD_User.sn)
      {
            #Write-Host "BINGO FOUND IT"
            $FlagLastName = 1
            Write-Host ""
            Write-Host "==== FOUND THE FOLLOWING EMPLOYEE IN ACTIVE DIRECTORY =="
            Write-Host "LASTNAME: $($AD_USER.sn)"
            Write-Host "FIRST NAME: $($AD_USER.givenName)"
            Write-Host "DEPARTMENT: $($AD_USER.department)"
            Write-Host "TITLE: $($AD_USER.title)"
            Write-Host "TELEPHONE NUMBER: $($AD_USER.telephoneNumber)"
            Write-Host "EMPLOYEE ID: $($AD_USER.employeeID)"
            Write-Host "EMPLOYEE NUMBER: $($AD_USER.employeeNumber)"
            Write-Host "================================================================"
            Write-Host ""
            $RightWrong = Read-Host 'NOTE: Is this the right   Employee, You are working on? (Y/N)'
            Write-Host ""
	    }
           If ($RightWrong -eq "N") {
                  [void]$foreach.moveNext()
             }
            
	   If ($RightWrong -eq "Y"){
												
	   $YesNo = Read-Host 'NOTE: Is the EMPLOYEE Information FOUND In ACTIVE DIRECTORY CORRECT AGAINST The EMPLOYEE Information FOUND In the   File? (Y/N)'
           Write-Host ""
         #Write-Host "YesNo Value: $($YesNo)"
            
	  If ($YesNo -eq "N")
               {
               Write-Host "Which of the following Employee information would you like to change in Active Directory ?"
               Write-Host ""
               $AD_User_Change = Read-Host '1 = LASTNAME. 2 = FIRST NAME. 3 = DEPARTMENT. 4 = TITLE. 5 = TELEPHONE NUMBER. 6 = EMPLOYEE ID. 7 = EMPLOYEE NUMBER'
                }
            switch ($AD_User_Change) {
             1 {
                  Write-Host "1"
                  $InputLastName = Read-Host 'Type the new Last Name:'
                  Write-Host ""
                  $NewLastName = Read-Host "Are you sure, you want to replace the following lastname ""$($AD_User.sn)"" by ""$InputLastName"" (Y/N)"
                  if (($NewLastName -eq "y"))
                     {
                        Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -surname $InputLastName -DisplayName $($InputLastName+", "+$AD_USER.givenName) -ErrorAction SilentlyContinue -ErrorVariable Err1
                        Start-Sleep -Seconds 5
                        $NewName = $InputLastName+", "+$AD_USER.givenName
                        Get-ADUser $AD_User.sAMAccountName  | Rename-ADObject -newname $NewName
                        Start-Sleep -Seconds 5
                        Write-Host "New LastName SET"
                        Start-Sleep -Seconds 30
                    }
              }
             2 {Write-Host "2"}
             3 {Write-Host "3"}
             4 {Write-Host "4"}
             5 {Write-Host "5"}
             6 {Write-Host "6"}
             7 {Write-Host "7"}
            }
            Start-Sleep -Seconds 30
      }

} 

Open in new window

Subash SundharanIT Infrastructure Architect
CERTIFIED EXPERT

Commented:
Add $file[0].sn -eq $AD_User.sn in third If statement..

If ($RightWrong -eq "Y" -and $file[0].sn -eq $AD_User.sn)
namergSystems Administrator

Author

Commented:
Hmm, I have to change little bit the logic,

      Elseif ($RightWrong -eq "N")
                        {
                        [void]$foreach.moveNext()
                        Write-Host "Moving to the next user"
                        Start-Sleep -Seconds 30
                        }
                  else
                        {
                        Write-Host "USER DOES NOT EXIST"
                        }
      } #Closes if ($file[0].sn -eq $AD_User.sn)
}closes For statement

But I get the following error:

Elseif : The term 'Elseif' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path
that the path is correct and try again.
At C:\scripts\AD\test.ps1:90 char:4
+             Elseif ($RightWrong -eq "N")
+             ~~~~~~
    + CategoryInfo          : ObjectNotFound: (Elseif:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException


                [void]$foreach.moveNext()
                Write-Host "Moving to the next user"
                Start-Sleep -Seconds 30

else : The term 'else' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
the path is correct and try again.
At C:\scripts\AD\test.ps1:96 char:4
+             else
+             ~~~~
    + CategoryInfo          : ObjectNotFound: (else:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException


                Write-Host "USER DOES NOT EXIST"
namergSystems Administrator

Author

Commented:
I think is a bug on PowerShell v3..hmmm....
Subash SundharanIT Infrastructure Architect
CERTIFIED EXPERT

Commented:
Elseif ($RightWrong -eq "N")
                        {
                        [void]$foreach.moveNext()
                        Write-Host "Moving to the next user"
                        Start-Sleep -Seconds 30
                        }
                  else
                        {
                        Write-Host "USER DOES NOT EXIST"
                        }

Elseif here doesn’t make any sense to me..

You can try something like..
$adsearch = $ad | Where {$file[0].sn -eq $_.sn}
If ($adsearch -eq $null)
{Write-Host "USER DOES NOT EXIST"}
Else{"Start your script"}

Open in new window


Did you get any error for the code which I suggested? or is it not working as expected?
Subash SundharanIT Infrastructure Architect
CERTIFIED EXPERT

Commented:
And regarding PS 3.0 if bug, If I am not wrong It only fails if the closing curly brace comes in same line with if keyword..
namergSystems Administrator

Author

Commented:
Hi Subsun I just got to the office, let me try.
namergSystems Administrator

Author

Commented:
Subsun, I get the following error:

Elseif : The term 'Elseif' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, veri
that the path is correct and try again.

At C:\scripts\AD\test.ps1:88 char:4
+             Elseif ($RightWrong -eq "N")
+             ~~~~~~
    + CategoryInfo          : ObjectNotFound: (Elseif:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
                Write-Host "Moving to the next user"
                #[void]$foreach.moveNext()
                #Write-Host "Moving to the next user"
                #Start-Sleep -Seconds 30
else : The term 'else' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify t
the path is correct and try again.
At C:\scripts\AD\test.ps1:95 char:4
+             else
+             ~~~~
    + CategoryInfo          : ObjectNotFound: (else:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException


                Write-Host "USER DOES NOT EXIST"

Code:

$YesNo = Read-Host 'NOTE: Is the EMPLOYEE Information FOUND In ACTIVE DIRECTORY CORRECT AGAINST The EMPLOYEE Information FOUND In the File? (Y/N)'
            Write-Host ""
            #Write-Host "YesNo Value: $($YesNo)"
            #if (($RightWrong -eq "Y") -and ($YesNo -eq "N"))
            if ($RightWrong -eq "Y")
             {
                        Write-Host "Which of the following Employee information would you like to change in Active Directory ?"
                        Write-Host ""
                        $AD_User_Change = Read-Host '1 = LASTNAME. 2 = FIRST NAME. 3 = DEPARTMENT. 4 = TITLE. 5 = TELEPHONE NUMBER. 6 = EMPLOYEE ID. 7 = EMPLOYEE NUMBER'
                  
                  switch ($AD_User_Change)
                  {
                        1 {
                              Write-Host "1"
                              $InputLastName = Read-Host 'Type the new Last Name:'
                              Write-Host ""
                              $NewLastName = Read-Host "Are you sure, you want to replace the following lastname ""$($AD_User.sn)"" by ""$InputLastName"" (Y/N)"
                              if (($NewLastName -eq "y"))
                              {
                                    Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -surname $InputLastName -DisplayName $($InputLastName+", "+$AD_USER.givenName) -ErrorAction SilentlyContinue -ErrorVariable Err1
                                    Start-Sleep -Seconds 5
                                    $NewName = $InputLastName+", "+$AD_USER.givenName
                                    Get-ADUser $AD_User.sAMAccountName  | Rename-ADObject -newname $NewName
                                    Start-Sleep -Seconds 5
                                    Write-Host "New LastName SET"
                                    Start-Sleep -Seconds 30
                              }
                        }

                  }
             }
             Start-Sleep -Seconds 30      
             Elseif ($RightWrong -eq "N")
                      {
                        Write-Host "Moving to the next user"
                        #[void]$foreach.moveNext()
                        #Write-Host "Moving to the next user"
                        #Start-Sleep -Seconds 30
                        }
             else
                        {
                        Write-Host "USER DOES NOT EXIST"
                        }
Subash SundharanIT Infrastructure Architect
CERTIFIED EXPERT

Commented:
Can you try starting Curly Brace in same line with IF statement?
Elseif ($RightWrong -eq "N") {
namergSystems Administrator

Author

Commented:
:( same thing.
namergSystems Administrator

Author

Commented:
I think I am going to give up for this part using powershell. :( I will do this first part of the project in C# then the second part in powershell.
Subash SundharanIT Infrastructure Architect
CERTIFIED EXPERT

Commented:
I will check in PS 3.0 and let you know..
namergSystems Administrator

Author

Commented:
ok thanks a lot.
namergSystems Administrator

Author

Commented:
Subsun,

Should I use powershell v2 ?
Subash SundharanIT Infrastructure Architect
CERTIFIED EXPERT

Commented:
I Actually tried Elseif in PS 3.0 and it's working for me.. I will take a close look at your code as soon as I get some time..:-)
namergSystems Administrator

Author

Commented:
hmm ok... I will paste the code one more time.
Subash SundharanIT Infrastructure Architect
CERTIFIED EXPERT

Commented:
it's fine, i can take it from old post..
namergSystems Administrator

Author

Commented:
Here it goes:

****************************************

Clear-Host
$FILE = Import-Csv "c:\scripts\ad\temp\FILE.csv" -header sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
$ad = Get-ADUser -filter * -Properties sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber  -SearchBase "OU=COMPANY,DC=COMPANYcolo,DC=pvt" | Select-Object  sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
Write-Host "Querying COMPANY Employee from FILE file against Active Directory"
Write-Host ""
Write-Host "++ FOUND THE FOLLOWING EMPLOYEE IN FILE FILE +++++++++++++++"
#Write-Host ""
Write-Host "LASTNAME: $($FILE[0].sn)"
Write-Host "FIRST NAME: $($FILE[0].givenName)"
Write-Host "DEPARTMENT: $($FILE[0].department)"
Write-Host "TITLE: $($FILE[0].title)"
Write-Host "TELEPHONE NUMBER: $($FILE[0].telephoneNumber)"
Write-Host "FLEX ID: $($FILE[0].employeeID)"
Write-Host "CLOCK NUMBER: $($FILE[0].employeeNumber)"
#Write-Host ""
Write-Host"+++++++++++++++++++++++++++++++++++++++++"
#Write-Host "Waiting for 5 seconds"
Start-Sleep -Seconds 5
foreach ($AD_User in $ad) {
      if ($FILE[0].sn -eq $AD_User.sn)
      {
            #Write-Host "BINGO FOUND IT"
            $FlagFILELastName = 1
            Write-Host ""
            Write-Host "==== FOUND THE FOLLOWING EMPLOYEE IN ACTIVE DIRECTORY =="
            #Write-Host ""
            Write-Host "LASTNAME: $($AD_USER.sn)"
            Write-Host "FIRST NAME: $($AD_USER.givenName)"
            Write-Host "DEPARTMENT: $($AD_USER.department)"
            Write-Host "TITLE: $($AD_USER.title)"
            Write-Host "TELEPHONE NUMBER: $($AD_USER.telephoneNumber)"
            Write-Host "EMPLOYEE ID: $($AD_USER.employeeID)"
            Write-Host "EMPLOYEE NUMBER: $($AD_USER.employeeNumber)"
            #Write-Host ""
            Write-Host "==============================================================="
            Write-Host ""
            #$RightWrong = Read-Host 'NOTE: Is this the right COMPANY Employee, You are working on? (Y/N)'
            Write-Host ""
            #Write-Host "RightWrong Value: $($RightWrong)"
            $YesNo = Read-Host 'NOTE: Is the EMPLOYEE Information FOUND In ACTIVE DIRECTORY CORRECT AGAINST The EMPLOYEE Information FOUND In the FILE File? (Y/N)'
            Write-Host ""
            #Write-Host "YesNo Value: $($YesNo)"
            #if (($RightWrong -eq "Y") -and ($YesNo -eq "N"))
            if ($RightWrong -eq "Y") {
                        Write-Host "Which of the following Employee information would you like to change in Active Directory ?"
                        Write-Host ""
                        $AD_User_Change = Read-Host '1 = LASTNAME. 2 = FIRST NAME. 3 = DEPARTMENT. 4 = TITLE. 5 = TELEPHONE NUMBER. 6 = EMPLOYEE ID. 7 = EMPLOYEE NUMBER'
                  
                  switch ($AD_User_Change)
                  {
                        1 {
                              Write-Host "1"
                              $InputLastName = Read-Host 'Type the new Last Name:'
                              Write-Host ""
                              $NewLastName = Read-Host "Are you sure, you want to replace the following lastname ""$($AD_User.sn)"" by ""$InputLastName"" (Y/N)"
                              if (($NewLastName -eq "y"))
                              {
                                    Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -surname $InputLastName -DisplayName $($InputLastName+", "+$AD_USER.givenName) -ErrorAction SilentlyContinue -ErrorVariable Err1
                                    Start-Sleep -Seconds 5
                                    $NewName = $InputLastName+", "+$AD_USER.givenName
                                    Get-ADUser $AD_User.sAMAccountName  | Rename-ADObject -newname $NewName
                                    Start-Sleep -Seconds 5
                                    Write-Host "New LastName SET"
                                    Start-Sleep -Seconds 30
                              }
                        }
                        2 {Write-Host "2"}
                        3 {Write-Host "3"}
                        4 {Write-Host "4"}
                        5 {Write-Host "5"}
                        6 {Write-Host "6"}
                        7 {Write-Host "7"}
                  }
             }
             Start-Sleep -Seconds 30      
             Elseif ($RightWrong -eq "N")
                      {
                        Write-Host "Moving to the next user"
                        #[void]$foreach.moveNext()
                        #Write-Host "Moving to the next user"
                        #Start-Sleep -Seconds 30
                        }
             else
                        {
                        Write-Host "USER DOES NOT EXIST"
                        }
      }
}
*********************************************************************
IT Infrastructure Architect
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
namergSystems Administrator

Author

Commented:
^&%$# it worked... PERFECT

But I will try something else. I have a meeting in 5 min and I will get back to you.
namergSystems Administrator

Author

Commented:
Subsun, you are my hero :)
I need to implement it for the other user properties as you saw in the code. If i get stuck more question on the way.

Thanks for your help, G

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.