Set Home directory from CSV file

Welcome Forums General PowerShell Q&A Set Home directory from CSV file

This topic contains 6 replies, has 4 voices, and was last updated by

3 years, 2 months ago.

  • Author
  • #35616

    Topics: 5
    Replies: 13
    Points: 0
    Rank: Member

    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?

  • #35617

    Topics: 5
    Replies: 13
    Points: 0
    Rank: Member

  • #35632

    Topics: 23
    Replies: 138
    Points: 245
    Helping Hand
    Rank: Participant

    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:
    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

  • #35636

    Topics: 6
    Replies: 236
    Points: 0
    Rank: Member

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

    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.

  • #35637

    Topics: 5
    Replies: 13
    Points: 0
    Rank: Member

    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.

  • #35665

    Topics: 5
    Replies: 13
    Points: 0
    Rank: Member

    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

  • #35712

    Topics: 4
    Replies: 57
    Points: 3
    Rank: Member

    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

The topic ‘Set Home directory from CSV file’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort