# A way to skip accounts that already exist?

**URL:** <https://forums.powershell.org/t/a-way-to-skip-accounts-that-already-exist/12406>\
**Category:** PowerShell Help\
**Created:** [April 16, 2019, 12:48pm UTC](https://forums.powershell.org/t/a-way-to-skip-accounts-that-already-exist/12406 "2019-04-16T12:48:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nelsonsaenz](https://avatars.discourse-cdn.com/v4/letter/n/7ab992/32.png) [@nelsonsaenz](https://forums.powershell.org/u/nelsonsaenz)\
**Post date:** [April 16, 2019, 12:48pm UTC](https://forums.powershell.org/t/a-way-to-skip-accounts-that-already-exist/12406/1 "2019-04-16T12:48:32Z")

</div>

Hey everyone,

Hoping someone can help with this. I have the command below that copies users in an OU to a security group…

Get-ADUser -SearchBase “OU=Marine Group,OU=New OUs,DC=corpycorp,DC=local” -Filter \* | % {Add-ADGroupMember ‘marinegroupmfa’ -Members $\_}

Works great and I want to automate this as a scheduled task so that anytime a new user is added, it will then also be added to my AD group. The problem is that every time I run it, I get this error stating all the existing accounts already exist, x 100 or so depending on number of users in the OU…

Add-ADGroupMember : The specified account name is already a member of the group

At line:1 char:103

- … C=com" -Filter \* | % {Add-ADGroupMember ‘BoulderCorpMFA’ -Members $\_}

- 

```
                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

```

&nbsp;

It still runs fine but I’d like for it to run clean and only copy newly added users. Is there a way to do this?

Thanks in advance!

Nelson

---

<div class="post-metadata">

**Author:** ![pwshliquori](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/pwshliquori/32/222_2.png) [@pwshliquori](https://forums.powershell.org/u/pwshliquori)\
**Post date:** [April 16, 2019, 1:02pm UTC](https://forums.powershell.org/t/a-way-to-skip-accounts-that-already-exist/12406/2 "2019-04-16T13:02:51Z")

</div>

Try/Catch block should do the trick. In your catch block, use Write-Warning or Write-Output so the script does not terminate and continues to try to add new members to the group.

```
Try {
    Get-ADUser -SearchBase "OU=Marine Group,OU=New OUs,DC=corpycorp,DC=local" -Filter * | 
    ForEach-Object {
        Add-ADGroupMember 'marinegroupmfa' -Members $_ -ErrorAction Stop
    }
}
Catch {
     Write-Warning "User already exists" # Or Write-Output
}
```

pwshliquori

---

<div class="post-metadata">

**Author:** ![nelsonsaenz](https://avatars.discourse-cdn.com/v4/letter/n/7ab992/32.png) [@nelsonsaenz](https://forums.powershell.org/u/nelsonsaenz)\
**Post date:** [April 16, 2019, 1:23pm UTC](https://forums.powershell.org/t/a-way-to-skip-accounts-that-already-exist/12406/3 "2019-04-16T13:23:04Z")

</div>

Thanks! I just tried running it and the script appears to stop. Removed a few users as a test and it does not re add the users.

---

<div class="post-metadata">

**Author:** ![pwshliquori](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/pwshliquori/32/222_2.png) [@pwshliquori](https://forums.powershell.org/u/pwshliquori)\
**Post date:** [April 16, 2019, 2:13pm UTC](https://forums.powershell.org/t/a-way-to-skip-accounts-that-already-exist/12406/4 "2019-04-16T14:13:54Z")

</div>

Sorry. Gave you the wrong syntax.

```
Get-ADUser -SearchBase "OU=Marine Group,OU=New OUs,DC=corpycorp,DC=local" -Filter * |
ForEach-Object {
    Try {
        Add-ADGroupMember 'marinegroupmfa' -Members $_ -ErrorAction Stop
    }
    Catch {
        Write-Warning "User already exists" # Or Write-Output
    }
}
```

---

<div class="post-metadata">

**Author:** ![nelsonsaenz](https://avatars.discourse-cdn.com/v4/letter/n/7ab992/32.png) [@nelsonsaenz](https://forums.powershell.org/u/nelsonsaenz)\
**Post date:** [April 16, 2019, 5:14pm UTC](https://forums.powershell.org/t/a-way-to-skip-accounts-that-already-exist/12406/5 "2019-04-16T17:14:04Z")

</div>

Yes, now it ran correctly.

Thanks so much for your help!!!

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:32pm UTC](https://forums.powershell.org/t/a-way-to-skip-accounts-that-already-exist/12406/6 "2024-05-16T20:32:59Z")

</div>


