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
Still a newbie to PS
Thank you all for looking at this
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.givennameif($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 + " " + $employeeIDNew-Item -ItemType Directory -Path \\server\$folderNew-Item -ItemType Directory -Path \\server\$folder\folder1New-Item -ItemType Directory -Path \\server\$folder\folder2New-Item -ItemType Directory -Path \\server\$folder\folder3New-Item -ItemType Directory -Path \\server\$folder\folder4}
Replace the -Path with your own.
I try that in the morning and let you know
Thank you so much
Carpe Diem
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
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)
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.givennameif($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 + " " + $employeeIDNew-Item -ItemType Directory -Path \\server\$folderNew-Item -ItemType Directory -Path \\server\$folder\folder1New-Item -ItemType Directory -Path \\server\$folder\folder2New-Item -ItemType Directory -Path \\server\$folder\folder3New-Item -ItemType Directory -Path \\server\$folder\folder4}
the Employee ID is populated
I verified
let me try this again in the morning
if I run a script against its properties it shows , so it is populated
EmployeeID : 1458341541* made up
Try to replace:$employeeID = $user.properties.employeenowith$employeeID = $user.properties.employeenumberor$employeeID = $user.properties.employeeid