Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Powershell and Creating Groups using CSV

Status
Not open for further replies.

Absolution84

Technical User
Jun 6, 2012
2
0
0
GB
Hi everyone,

I am new on this forum and also to powershell.

I have a question which I cannot seem to find an answer for elsewhere on the web.

If I have a csv file which looks like.....

objectClass sAMAccountName DN
group Customer Services CN=Customer Services,OU=User Groups,dc=example,dc=co,dc=uk
group Finance CN=Finance,OU=User Groups,dc=example,dc=co,dc=uk
group Finance Managers CN=Finance Managers,OU=User Groups,dc=example,dc=co,dc=uk
group Sales CN=Sales,OU=User Groups,dc=example,dc=co,dc=uk

How do import groups using the revelant information i.e. sAMAccountName,DN?

I dont know many of the ins and outs of powershell import-csv so I'm a little stumped.

If someone could show me a a sample script or give some advice, I would really appreciate it.

Thank you in advance for any replies.


 
I haven't tested this, but here's the steps that should work

Code:
$domainName = ([System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()).Name
$domainName = $domainName -replace "\.", ",dc="
$ou = [ADSI] "LDAP://ou=User Groups,dc=$domainName"

$groups = Import-Csv mygroups.csv
ForEach ($group in $groups){
	$newgroup =  $ou.Create("group", "cn=$sAMAccountName")
	$newgroup.Put("SamAccountName", $sAMAccountName)
	$newgroup.Put("groupType", -2147483640)
	$newgroup.SetInfo()
}

Do you have your Tek-Tips.com Swag? I've got mine!

Stop by the new Tek-Tips group at LinkedIn.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top