Set Home directory from CSV file

Hi guys,

I have been struggling With this task for some time now.
I don’t understand how the CSV and foreach are working…

Here is the script

As you can see I’am importing a CSV file.

The csv file have a first column named Samaccountname
And underneath that in Excel the usernames are pasted in.

Can somebody help me With this?

well it looks like you have a couple of issues, first reading the $path comment
+$(“$”) this section portion looks wrong, you are trying to call a variable $
try $path=“\file02"+$($_.samAccountName)+”$"
that should result in a path of \file01\samaccountname$
(thats what it appears you are trying to set based upon my reading)

Secondly, per the TechNet for set-aduser for HomeDrive:
HomeDrive
Specifies a drive that is associated with the UNC path defined by the HomeDirectory property. The drive letter is specified as “:” where indicates the letter of the drive to associate. The must be a single, uppercase letter and the colon is required. This parameter sets the HomeDrive property of the user object. The LDAP Display Name (ldapDisplayName) for this property is “homeDrive”.

you need to change -HomeDrive $path to not use the Path variable, you need to provide a drive letter here

This works. But I agree with David – HomeDrive is supposed to be a drive letter.

# https://powershell.org/forums/topic/set-home-directory-from-csv-file/
Import-Csv -Path C:\Ephemeral\Import_AD_Users_TBO.csv | foreach {
    $san = $PSItem.SamAccountName
    $path = "\\file02\${san}$"
    Write-Verbose "HomeDirectory for $san is $path"
    Set-ADUser $san -HomeDrive $path -HomeDirectory $path
}

It really should be something like below. Read the help file for Set-ADUser.

Set-ADUser $san -HomeDrive "H:" -HomeDirectory $path

And by the way, you really should be exploring Group Policy Preferences for this setting in the future.

Hi.
Thanks for answering my question.
I’m going to test it in my LAB environment.
I’am fully aware of GPO.
But we are moving all Our files from fileserver to another fileserver
And we are not going to do it all at once.
And therefore we are creating an Excel file instead of using GPO.

This worked perfectly Bob,
I see how you use $PSitem.SamaccountName
Is that the same as using $_.samAccountName when reffering to a csv column.
I like how simple Your code seem’s to be
Do you have a blog or some forum content online?

Best regard
Nicolai Magnussen

Nicolai, you are correct they are the same thing. PSv3 introduced alias psitem for the mystic underscore.

$_
Same as $PSItem. Contains the current object in the pipeline object.
You can use this variable in commands that perform an action on every
object or on selected objects in a pipeline.

There is bunch of other automatic variables which you can look up with command. Lot of cool stuff there!

Get-Help about_Automatic_Variables -ShowWindow