script to extract info out of AD users and create folders with it

rated by 0 users
This post has 7 Replies | 2 Followers

Not Ranked
Posts 6
Carpe_Diem Posted: 05-02-2012 10:00 AM

I am trying to create a script which will:

Create a folder structure on a network drive using each user name (First last and middle initial if exist and Employee ID ) pulling information out of existing AD Users

Within each folder it should create 4 subfolders ( all the same)

So as example: by gathering user information from AD the folder should look like this

USERS

---------- Meyers  Tim J 4578744  

------------------------folder one

------------------------folder two

------------------------folder three

------------------------folder four

 ---------- Kruger Paul S 457489  

------------------------folder one

------------------------folder two

------------------------folder three

------------------------folder four

Still a newbie to PS  Sad

Thank you all for looking at this Smile

Top 25 Contributor
Posts 287
Top Contributor

I haven't tested it but this should do it:

$dom = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$root = $dom.GetDirectoryEntry()
$search = [System.DirectoryServices.DirectorySearcher]$root
$search.Filter = "(objectCategory=User)"
$result = $search.FindAll()

foreach($user in $result){

$givenname = $user.properties.givenname

if($givenname.properties.displayname[0].split(" ").count -eq 2)
{$givenname = $givenname.properties.displayname[0].split(" ")[0] + " " + $givenname.properties.displayname[0].split(" ")[1][0]}

$surname = $user.properties.sn
$employeeID = $user.properties.employeeno
$folder = $givenname + " " + $surname + " " + $employeeID

New-Item -ItemType Directory -Path \\server\$folder
New-Item -ItemType Directory -Path \\server\$folder\folder1
New-Item -ItemType Directory -Path \\server\$folder\folder2
New-Item -ItemType Directory -Path \\server\$folder\folder3
New-Item -ItemType Directory -Path \\server\$folder\folder4
}

Replace the -Path with your own.

Not Ranked
Posts 6

I try that in the morning and let you know

Thank you so much

Carpe Diem

Not Ranked
Posts 6

Thank you for your help

Here is what I tried

$dom = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()

$root = $dom.GetDirectoryEntry()$search = [System.DirectoryServices.DirectorySearcher]$root

$search.Filter = "(objectCategory=User)"

$result = $search.FindAll()

foreach($user in $result){

$givenname = $user.properties.givenname

if($givenname.properties.displayname[0].split(" ").count -eq 2)

{$givenname = $givenname.properties.displayname[0].split(" ")[0] + " " + $givenname.properties.displayname[0].split(" ")[1][0]}

$surname = $user.properties.sn

$employeeID = $user.properties.employeeno

$folder = $surname + " " + $givenname + " " + $employeeID

# replace excess whitespace

$folder -replace "\s{2,}", " "

New-Item -ItemType Directory -Path C:\test\$folder

New-Item -ItemType Directory -Path C:\test\$folder\folder1

New-Item -ItemType Directory -Path C:\test\$folder\folder2

New-Item -ItemType Directory -Path C:\test\$folder\folder3

New-Item -ItemType Directory -Path C:\test\$folder\folder4

}

I get

Directory: C:\test

Mode LastWriteTime Length Name

---- ------------- ------ ----

d---- 5/7/2012 1:12 PM Test User

Directory: C:\test\Test User

Mode LastWriteTime Length Name

---- ------------- ------ ----

d---- 5/7/2012 1:12 PM folder1

d---- 5/7/2012 1:12 PM folder2

d---- 5/7/2012 1:12 PM folder3

d---- 5/7/2012 1:12 PM folder4

Cannot index into a null array.

At line:11 char:4

+ if($givenname.properties.displayname[0].split(" ").count -eq 2)

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : InvalidOperation: (:) [], RuntimeException

+ FullyQualifiedErrorId : NullArray

 

 

 

So it does work partially

It creates a folder with each users Last and first name (leaves all the white spaces in place,

No middle initials and no employee ID at the end)

Top 25 Contributor
Posts 287
Top Contributor

I know what's wrong, but I don't know why it's not displaying the employee ID.. Perhaps the property is empty.

$dom = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$root = $dom.GetDirectoryEntry()
$search = [System.DirectoryServices.DirectorySearcher]$root
$search.Filter = "(objectCategory=User)"
$result = $search.FindAll()

foreach($user in $result){

$givenname = $user.properties.givenname

if($user.properties.displayname[0].split(" ").count -eq 2)
{$givenname = $user.properties.displayname[0].split(" ")[0] + " " + $user.properties.displayname[0].split(" ")[1][0]}

$surname = $user.properties.sn
$employeeID = $user.properties.employeeno
$folder = $givenname + " " + $surname + " " + $employeeID

New-Item -ItemType Directory -Path \\server\$folder
New-Item -ItemType Directory -Path \\server\$folder\folder1
New-Item -ItemType Directory -Path \\server\$folder\folder2
New-Item -ItemType Directory -Path \\server\$folder\folder3
New-Item -ItemType Directory -Path \\server\$folder\folder4
}


Not Ranked
Posts 6

the Employee ID is populated

I verified

 let me try this again in the morning

 

Not Ranked
Posts 6

if I run a script against its properties  it shows , so it is populated

EmployeeID : 1458341541*   made up

 

Top 25 Contributor
Posts 287
Top Contributor

Try to replace:

$employeeID = $user.properties.employeeno

with

$employeeID = $user.properties.employeenumber

or

$employeeID = $user.properties.employeeid

 

Page 1 of 1 (8 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.