Few of the articles are posted as Images, Please use Laptop / Computers to go through the articles for best experience. For phone users, switch to Web Version

Please Share with your colleagues if you found these blogs informative. Happy Learning :-)

Powershell Script - Creating New AD Groups

#Author - Abhishek Bansal 

$grp = Get-Content ./Input.txt
foreach($row in $grp){
$row = $row.trim()

    try
    {
    New-ADGroup -Name $row -GroupScope Global -GroupCategory Security -Path "OU=Groups,OU=Production,DC=Mari,DC=com" -PassThru | Select Name,@{n=("Status");e={("Created under")}},DistinguishedName | Export-Csv ./Output.csv -Append -NoTypeInformation
    }

catch
    {
       $row | Select-Object -Property @{n=("Name");e={($row)}},@{n=("Status");e={("already exist under ")}},@{n="DistinguishedName";e={(Get-ADGroup -Identity $row | Select DistinguishedName)}} | Export-Csv ./Output.csv -Append -NoTypeInformation
        }

}



Read Me !!

1. I am using a relative path to take input & export the output. So please make sure you Create a folder with any name of your liking, then copy the above script & save it with .ps1 extension into the folder.

2. Once copied, also create a text file name Input.txt ( This will be used to take input into our Script & it should contain AD Group names which we need to create.



3. Once done, run the script. It will create the new AD Group under OU mentioned in the script.














Please note- Some Arguments are static in the Script, they will definitely vary in your workspace, so  edit them as per your structure.

Arguments required editing - Group Scope / Category [ If you want some other Scope & Category ], Path - [ OU  Location where you want to create the Groups]

4. Also, If a Group is already created, then this script will through an error & for that we have mentioned Catch block. This way you can see which all Groups are created & which are already present, where it is present etc.



















Finish

No comments:

Post a Comment