Avatar of namerg
namerg
Flag for United States of America asked on

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

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,
PowershellActive DirectoryScripting Languages

Avatar of undefined
Last Comment
namerg

8/22/2022 - Mon
SubSun

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

Open in new window

namerg

ASKER
Hey Guru Subsun,

But will it not break the "for" statement ?
SOLUTION
SubSun

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
namerg

ASKER
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) {....}
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
namerg

ASKER
By the way this is all related to my previous questions, it is like a mini application but with poweshell.
SubSun

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

namerg

ASKER
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>
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SubSun

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..
namerg

ASKER
Correct. The result of that of IF is, 1, True.
But think about that can be more than one User with a lastname Silveira.
SubSun

[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..
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
namerg

ASKER
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
      }

}
SubSun

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

SubSun

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

If ($RightWrong -eq "Y" -and $file[0].sn -eq $AD_User.sn)
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
namerg

ASKER
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"
namerg

ASKER
I think is a bug on PowerShell v3..hmmm....
SubSun

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?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
SubSun

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..
namerg

ASKER
Hi Subsun I just got to the office, let me try.
namerg

ASKER
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"
                        }
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SubSun

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

ASKER
:( same thing.
namerg

ASKER
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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
SubSun

I will check in PS 3.0 and let you know..
namerg

ASKER
ok thanks a lot.
namerg

ASKER
Subsun,

Should I use powershell v2 ?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SubSun

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..:-)
namerg

ASKER
hmm ok... I will paste the code one more time.
SubSun

it's fine, i can take it from old post..
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
namerg

ASKER
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"
                        }
      }
}
*********************************************************************
ASKER CERTIFIED SOLUTION
SubSun

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
namerg

ASKER
^&%$# it worked... PERFECT

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

ASKER
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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.