By MoNeY2OverFlow

2018-11-08 19:25:33 8 Comments

I am building a PowerShell script to create AD Groups (Global and DomainLocal) by Importing their names from a Csv file. I am having a hard time handling exceptions that will be generated in case Groups already exist. What I want to achieve is if the Groups do not exist by the name in Csv then PS should create them and show message "Groups have been created" and if they already exist then it should display "Groups already exist" line by line so that if one exists and the other one doesn't then it should display the corresponding message.

What is happening is that PS doesn't display a message when it has created groups and when exception does occur it displays message only for Global Group not Local.

Please advise

Here's the code -

Try {
    New-ADGroup -Name TestGlobal -GroupCategory Security -GroupScope Global -ManagedBy TEMP01 -Description "Owner is TEMP01" -Path (Some OU)
} Catch [Microsoft.ActiveDirectory.Management.ADException] {
    if ($_ -like "The specified group already exists") {
        Write-Host “!!! GLOBAL GROUP ALREADY EXISTS !!!”
    } elseif ($_ -eq $null) {
Try {
    New-ADGroup -Name TestLocal -GroupCategory Security -GroupScope DomainLocal -ManagedBy TEMP02 -Description "Owner is TEMP02" -Path (Some OU)
} Catch [Microsoft.ActiveDirectory.Management.ADException] {
    if ($_ -like "The specified group already exists") {
        Write-Host “!!! LOCAL GROUP ALREADY EXISTS !!!”
    } elseif ($_ -eq $null) {


@Gabriel Luci 2018-11-09 15:52:47

PowerShell is a bit strange in that, by default, errors are non-terminating. That means that errors will be output to the console, but it will just continue on to the next line of code as if nothing happened.

Unfortunately, Try blocks only respond to terminating errors.

You can change this behaviour. On both of your New-ADGroup lines, add this to the end:

-ErrorAction Stop

That will tell PowerShell that you want it to treat errors on that line as terminating.

If you want, you can do some more reading about it here:

@MoNeY2OverFlow 2018-11-09 16:53:01

I solved the issue for now. Thanks for the suggestion !!

Related Questions

Sponsored Content

22 Answered Questions

[SOLVED] PowerShell says "execution of scripts is disabled on this system."

17 Answered Questions

[SOLVED] Determine installed PowerShell version

  • 2009-12-01 11:30:03
  • MagicAndi
  • 2560603 View
  • 2303 Score
  • 17 Answer
  • Tags:   powershell

7 Answered Questions

[SOLVED] How do you comment out code in PowerShell?

13 Answered Questions

[SOLVED] How to run a PowerShell script

1 Answered Questions

[SOLVED] How to handle command-line arguments in PowerShell

1 Answered Questions

[SOLVED] Powershell with Active Directory creating groups

2 Answered Questions

[SOLVED] Warning when adding new aduser to group

0 Answered Questions

1 Answered Questions

Powershell script to list all users whose manager account is disabled

  • 2015-10-01 09:18:32
  • Bunny
  • 909 View
  • 0 Score
  • 1 Answer
  • Tags:   powershell

1 Answered Questions

[SOLVED] Suppress Exception in Office365 powershell

Sponsored Content