locked
delete group that contains a special name RRS feed

  • Question

  • Hi

    im trying to delete Groups that contain special names. What im trying to do is to check first if there are such Groups and then pipe the deletion into the get-adgroup. If there are no Groups found it exit (i dont want the script to go through the deletion part). I cant get the foreach loop to be piped after get-adgroup. any help will be appreciated.

    #Remove Active Directory groups
    $Server = "SERVER01"
    
    $X = Adminitrator_' + $Server + '*'
    try 
        {
            Get-ADGroup -Filter {Name -like $X} | Select-Object Name -ErrorAction Stop | foreach ($ADGroup in $ADGroups)
            {
                Write-Host $ADGroup.Name
                #Remove-ADGroup -Identity $ADGroup.Name
            }
        }
    catch
        {
        "the following error appeared: `"$($_.Exception.Message)`""
        }
    

    Tuesday, June 2, 2020 5:59 AM

Answers

  • You have to use "ForEach-Object" and a pipe symbol t pipe something.

    help about_Pipelines


    \_(ツ)_/

    • Marked as answer by ITinNeed Tuesday, June 2, 2020 11:37 AM
    Tuesday, June 2, 2020 6:10 AM