Link to home
Start Free TrialLog in
Avatar of kuzum
kuzum

asked on

change home drives for .csv users

Dear Experts,

I have this below powershell code working for me and I am trying to change it to get user details from csv file instead but could not get it working.

(working code)

import-module activedirectory        

 $targetou= "OU=Test,OU=Users,OU=myOU,DC=myOU,DC=mydc,DC=local"
 Get-ADUser -Filter * -SearchBase $targetou | Foreach-Object{
 $sam = $_.SamAccountName
     Set-ADuser -Identity $_ -HomeDrive "Y:" -HomeDirectory "\\server\share\HOME\$sam"
     }
 "

 I am trying to get this working slightly different and get user list from csv file instead and change their home drive path in AD. Could you please help me with below code? as expected, it is not working. Could you please modify this or give another one that you used?

( broken code)
 $UserList = Import-Csv -Path C:\mydata\userlist.csv

 foreach ($User in $UserList) {
 
 $Account = Get-ADUser -LDAPFilter ('(displayname={0})'-f. $User.DisplayName);
  $HomeDirectory = '\\server\share\HOME'+-f $Account.SamAccountName;
    Set-ADuser -Identity $Account.SamAccountName -HomeDirectory $HomeDirectory  -HomeDrive -Y;
     }
Avatar of oBdA
oBdA

Try it like this:
$UserList = Import-Csv -Path C:\Temp\userlist.csv
ForEach ($User in $UserList) {
	If ($Account = Get-ADUser -LDAPFilter ("(displayname=$($User.DisplayName))")) {
		$HomeDirectory = "\\server\share\HOME\$($Account.SamAccountName)"
		"Processing $($User.DisplayName) ($($Account.SamAccountName)): new home '$($HomeDirectory)'"
		Set-ADuser -Identity $Account.SamAccountName -HomeDirectory $HomeDirectory -HomeDrive -Y
	}
}

Open in new window

Avatar of kuzum

ASKER

Hi Obda

received this error?

Get-ADUser : The search filter cannot be recognized
At line:4 char:27
+     If ($Account = Get-ADUser <<<<  -LDAPFilter ("(displayname=$($User.DisplayName))")) {
    + CategoryInfo          : NotSpecified: (:) [Get-ADUser], ADException
    + FullyQualifiedErrorId : The search filter cannot be recognized,Microsoft.ActiveDirectory.Management.Commands.GetADUser
 

____________________________________________________________________________________________________________________________________________________________________________________________________________________
Then your csv probably isn't a csv, so $User.DisplayName is $Null.
This will list the name it's currently processing:
$UserList = Import-Csv -Path C:\Temp\userlist.csv
ForEach ($User in $UserList) {
	"Processing '$($User.DisplayName)'" | Write-Host -Fore Yellow
	If ($Account = Get-ADUser -LDAPFilter ("(displayname=$($User.DisplayName))")) {
		$HomeDirectory = "\\server\share\HOME\$($Account.SamAccountName)"
		"Processing $($User.DisplayName) ($($Account.SamAccountName)): new home '$($HomeDirectory)'"
		Set-ADuser -Identity $Account.SamAccountName -HomeDirectory $HomeDirectory -HomeDrive -Y
	}
} 

Open in new window

Avatar of kuzum

ASKER

I guess I'm missing something in the code?

Unexpected token 'User' in expression or statement.
At line:6 char:17

Unexpected token '.DisplayName' in expression or statement.
At line:6 char:22

Missing closing '}' in statement block.
At line:6 char:34

Missing closing '}' in statement block.
At line:6 char:34

Unexpected token ')' in expression or statement.
At line:6 char:34
$UserList = Import-Csv -Path C:\mydata\userlist.csv
 foreach ($User in $UserList) {
  $Account = Get-ADUser -LDAPFilter ('(displayname={0})'-f. $User.DisplayName);
 $HomeDirectory = '\\server\share\HOME'+-f $Account.SamAccountName;
    Set-ADuser -Identity $Account.SamAccountName -HomeDirectory $HomeDirectory  -HomeDrive -Y;
     }

Open in new window


Is userlist.csv in fact a valid comma separated value file?
i.e.
colum1,colum2,colum3,DisplayName,anothercolumn,yetanothercolumn
,,,"don.brown",,
,,,"adam.west",,
The very minimum it would be
DisplayName
don.brown
adam.west

Line 1 is the header, lines 2+ are the data.
if you have just a list of names use get-content vice import-csv, and modify your script i.e.
in the get-aduser line replace  $user.DisplayName with just $user
Use this, this fixes the error with the home drive (the '-' in front was incorrect).
* Do not change anything below line 3.
* Use a real csv file with a header line and a column "DisplayName".
$HomeRoot = "\\server\share\HOME"
$UserList = Import-Csv -Path C:\Temp\userlist.csv
ForEach ($User in $UserList) {
	If ($Account = Get-ADUser -LDAPFilter "(displayname=$($User.DisplayName))") {
		$HomeDirectory = Join-Path -Path $HomeRoot -ChildPath $Account.SamAccountName
		"Processing $($User.DisplayName) ($($Account.SamAccountName)): new home '$($HomeDirectory)'"
		Set-ADuser -Identity $Account.SamAccountName -HomeDirectory $HomeDirectory -HomeDrive Y
	}
}

Open in new window

Avatar of kuzum

ASKER

thanks for your responses guys.

David, "parse errors detected" did not actually run the code.
user list is as follows;   ( no gabs no display name headers..)

don.brown
adam.west


OBda,  I guess I am doing something wrong if you guys tested your codes? thanks

Get-ADUser : The search filter cannot be recognized
At line:4 char:27
+     If ($Account = Get-ADUser <<<<  -LDAPFilter "(displayname=$($User.DisplayName))") {
    + CategoryInfo          : NotSpecified: (:) [Get-ADUser], ADException
    + FullyQualifiedErrorId : The search filter cannot be recognized,Microsoft.ActiveDirectory.Management.Commands.GetADUser
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kuzum

ASKER

David, oBdA

apologies, my fault. I did not read your comment properly.   David added -Header Displayname to your code and did not work. I appreciate your time and I would like to access both of yours solutions if you can corret my fault on yours please?

this worked for me. I will post one  more question please for a similar thing.
$HomeRoot = "\\server\share\HOME"
$UserList = Import-Csv -Path C:\Temp\userlist.csv -Header DisplayName
ForEach ($User in $UserList) {
      If ($Account = Get-ADUser -LDAPFilter "(displayname=$($User.DisplayName))") {
            $HomeDirectory = Join-Path -Path $HomeRoot -ChildPath $Account.SamAccountName
            "Processing $($User.DisplayName) ($($Account.SamAccountName)): new home '$($HomeDirectory)'"
            Set-ADuser -Identity $Account.SamAccountName -HomeDirectory $HomeDirectory -HomeDrive Y
      }
}