I'm writing a simple PowerShell script to get all the users from a specific OU in Active Directory (AD) and all the folders from a directory, then compare each username with each foldername to verify that the user has a folder there, and do something.
The script shown below works fine, but when it matches the strings, I want it to break and return to the outer For-EachObject
, switching the username. (It's not necessary to verify the rest of the list if it already found one), but the break is breaking the entire script, it just ends.
$PerfilFolder = Get-ChildItem $FileServerPath | Sort
Get-ADUser -SearchBase $SB -Filter * | Sort SamAccountName | % {
$UserName = $_.SamAccountName
$PerfilFolder | % {
if($UserName -match (($_.Name).Split(".")[0])){
#Match!
#Do something
break
}
}
}
I already tried return
and continue
, but all of them have the same behavior. What should I do?
Also general improvements tips are welcome :)