Thursday, June 28, 2012

Powershell scripts for AD-deployments

Some usefull scripts for AD-deployment in server 2008 r2 with powershell.

To Create homefolders:

$dataSource=import-csv “users.csv”
Set-Location "d:\data\users"

foreach($dataRecord in $datasource) {
$name=$dataRecord.FirstName + ”.” + $dataRecord.LastName
New-Item $name -type directory

To create the users:

#Change here to correspond to your domainenvironment

#Set homedir if needed remove if dont

$dataSource=import-csv “users.csv”
foreach($dataRecord in $datasource) {
$cn=$dataRecord.FirstName + ” ” + $dataRecord.LastName

#check if lastname is empty.
if (!$dataRecord.LastName) {
$dataRecord.LastName=" "
else {
$sAMAccountName=$dataRecord.FirstName + “.” + $dataRecord.LastName


write-host "Creating user :"$sAMAccountName

$displayName=$sn + “ ” + $givenName

$userPrincipalName=$sAMAccountName + “@sb.local”
#remove below if homedrive isn't used.
write-host "User :"$sAMAccountName" created"

write-host "Users created"


Above script imports users from csv file looking like this:



Also to set permissions the following might work, i’ve already forgotten. anyways it might almost work with some modifications.


#script to give full control NTFS permissions on a directory to the domain user with the same name of that directory

#script settings

$domain = “contoso.local”

$root = “d:\data\users\”

#don’t edit below here
$folders = Get-ChildItem $root

ForEach ($folder in $folders)


$username = $domain+“\”+$folder

$permissions = Get-Acl $folder

$userpermissions = New-Object System.Security.AccessControl.FileSystemAccessRule($username,“FullControl”, “ContainerInherit, ObjectInherit”, “None”, “Allow”)

Set-Acl $folder $permissions

Write-Host “Set permissions on $folder for $username”


I don’t take cred for creating the scripts from scratch. I found them on the net, but was in a rush at the time so i didn’t note the address. They weren’t perfect to begin with, but i’ve modified them so they work for my purposes.

next step would be to add them to groups auto, but this is step is fairly painless in the gui so we’ll hold of on that for the time being.

No comments: