Tuesday, August 4, 2015

Export Security Group Information from AD to CSV file

This Script exports Security Group Information from AD to CSV file

<#.Synopsis
This scripts exports Security Groups information from your active directory OU to CSV file 

Selected information: CN,Description,Info,distinguishedname.

.Description
Writen by Miikka Kallberg

.Parameter FilePath
Path to save the ouput  CSV File

.Parameter csvfile
Filename of the output CSV File name

.Parameter OU
Organization Unit where you want to get the user information

.Example
.\groupinfo.ps1 -FilePath c:\temp\ -csvfile marvin.csv -OU Groups
This will export Security Group information from Groups OU to c:\temp\marvin.csv 

#>
Param([String]
[Parameter(Mandatory=$true)]
$FilePath='',
[Parameter(Mandatory=$true)]
$csvfile='',
[Parameter(Mandatory=$true)]
$OU='')

# Import active directory module for running AD cmdlets
Import-Module activedirectory

#Get domain name
get-addomain | select distinguishedname -OutVariable Domainname

#Filter unneeded information from variable
foreach ($DN in $Domainname)
{

$domain = $Domainname.distinguishedname
}

# Create searchpath combining OU and domain name in correct format
$Oubase="OU=$OU,$Domain"

Get-ADgroup -filter * -properties CN,Description,Info,distinguishedname -SearchBase "$Oubase" | select CN,Description,Info,distinguishedname | ConvertTo-Csv | Out-File (Join-Path $filepath $csvfile)

No comments:

Post a Comment