sidfender
(sidfender)
1
I have a list of users to create in AD and I am using a CSV file with Powershell to import the users (import-csv | new aduser). I am trying to create the home drives that are mapped to a U: - ideally from a path defined in the csv file, as home path - the folder path example is
\test\users\users J\joe bloggs each users folder needs to be created in the folder that relates to their initial.
I have tried a few script with no luck -
Get-ADUser - | Foreach-Object{ $sam = $.SamAccountName Set-ADuser -Identity $ -HomeDrive “U:” -HomeDirectory “\Servername\folder\subfolder initial$($user.samaccountname)”
please can someone help and suggest a script- I am very new to powershell.
thanks
Sid
2 Spice ups
Neally
(Neally)
2
If you post code, please use the insert code button! Please and thank you!
sidfender
(sidfender)
3
Get-ADUser - | Foreach-Object{ $sam = $_.SamAccountName Set-ADuser -Identity $_ -HomeDrive "U:" -HomeDirectory "\\Servername\folder\subfolder initial\$($user.samaccountname)"
1 Spice up
Neally
(Neally)
4
How does your CSV look like?
The code you posted has nothing to do with the CSV really?
# this will run against ALL (*) AD USers
Get-ADUser -filter * |
Foreach{
Set-ADuser -Identity $_ -HomeDrive "U:" -HomeDirectory "\\Servername\folder\subfolder initial\$($_.samaccountname)"
}
sidfender
(sidfender)
5
The full code is at work I will post tomorrow - I have attached the CSV file - I have created the AD accounts - and manually input the U: but would be greatful for guidance on how to create a script to do it.
thanks
DoctorDNS
(DoctorDNS)
6
Welcome to the community.
One thing to add to your script - creating the home drive if it needs to be created:
$HomePath = join-path "U:\Servername\folder\subfolder Initial" "$user.SamAccountName"
If (! test-path $homepath)
{
New-Item -Path $homePath -itemtype Directory
}
sidfender
(sidfender)
7
Hi all,
Thank you for your help so far - please can I ask will the following code identify the correct location to create the user folder:
The folders are all ready created as per below:
e.g,
Domian\Server name\users\users B\ Blogs Test
Users -
A
Any Name
Andy Test
B
Bloggs Test
C
Carl Test
D
Can I not point the script to pick up the path from the CSV directly?
this will run against ALL (*) AD USers
Get-ADUser -filter * |
Foreach{
Set-ADuser -Identity $_ -HomeDrive “U:” -HomeDirectory “\Servername\folder\subfolder initial$($_.samaccountname)”
psophos
(M Boyle)
8
If the profile foeld in the csv file already has the correct path then you can just use it directly.
-HomeDirectory $_.profile
sidfender
(sidfender)
9
Sorry but the code will not insert using the code button
This script is work in progress have not got it working yet to create users.
Import-module activedirectory
#Import the list of users
$Users = Import-Csv -Path C:\Scripts\Userlist.csv
#Define variable for a server with AD web services installed
$ADServer = ‘test’
$location = “OU=test users,OU=TestUsers,DC=abcd,DC=abcds”
foreach ($User in $Users)
{
$UserFirstname = $User.Firstname
$Displayname = $User.Lastname + ", " + $User.Firstname
$UserLastname = $User.Lastname
$Description = $User.Description
$Office =$user.office
$phone = $User.phone
$email = $user.email
$Password = $User.Password
#Creation of the account with the requested formatting.
#Define samAccountName to use with NewADUser in the format firstName LastName
$sam = $GivenName + " " + $Surname
#Define domain to use for UserPrincipalName (UPN)
$Domain = ‘@test.test.uk’
#Define UerPrincipalname
$UPN = $sam + $Domain
Try { $nameinAD = Get-ADUser -server $ADServer -Credential $GetAdminact -LDAPFilter “(sAMAccountName=$sam)” }
Catch { }
If(!$nameinAD)
{
$i++
New-ADUser -Name “$Displayname” -DisplayName “$Displayname” -UserPrincipalName $UPN -GivenName “$UserFirstname” -Surname “$UserLastname” -Description “$Description” -Office “Office” -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -Enabled $true -Path “$OU” -ChangePasswordAtLogon $True –PasswordNeverExpires $false -server server.domain.local
$Displayname
}
sidfender
(sidfender)
10
Hi M Boyle is that the only line I need to insert in the script?
thank you
sidfender
(sidfender)
11
forgot to add - do I need to also define the drive letter:
#Set homeDrive for each user
$homedrive = “U”
psophos
(M Boyle)
12
Pretty sure you need both of them.
Err, I’m confused. Are you adding new users or changing existing ones?
To change existing users this might work:
Import-Csv 'c:\home_drives.csv' |
Foreach-Object { Set-ADuser -Identity $_.SamAccountName -HomeDrive "U:" -HomeDirectory $_.profile -WhatIf }
delete the -Whatif if things look ok.
jitensh
(JitenSh)
13