Few weeks back, I got a task to create multiple AD Groups from a CSV file. Below is a dummy look of a CSV that i got.
1. Make sure columns heading mentioned in Input file should not be altered.
2. In below case, i am taking details such as Group name, Path, scope description from Input.csv.
3. Logs_date.csv file can also be referred for detailed output.
***********************************************************************#>
$groupdetails = Import-Csv .\Input.csv
$date = $(Get-Date -Format "dd_MM_yy")+".csv"
foreach($group in $groupdetails)
{
try
{
New-ADGroup -Name $group.Name -GroupScope $group.Scope -GroupCategory $group.Category -Path $group.Path -Description $group.Description -PassThru | Select Name,@{n=("Status");e={("Created under")}},DistinguishedName | Export-Csv ./Logs_$date -Append -NoTypeInformation
}
catch
{
$group | Select-Object -Property @{n=("Name");e={($group.Name)}},@{n=("Status");e={("already exist under ")}},@{n="DistinguishedName";e={((Get-ADGroup -Identity $group.Name).DistinguishedName)}} | Export-Csv ./Logs_$date -Append -NoTypeInformation
}
}
Sample Output after Groups creation
No comments:
Post a Comment