Bulk change HomeDirectory for AD-Accounts

This topic contains 2 replies, has 2 voices, and was last updated by  Alex Reil 1 week, 5 days ago.

  • Author
  • #102085

    Alex Reil


    i am struggling with a apparently easy task: Changing all entries in ad-accounts with Homedir beginning with \\oldserver\.. to \\newserver\...

    This is what I expect to work:

    get-aduser -Filter { SamAccountName -like "seppl" } -SearchBase "OU=some, OU=entries, DC=here, DC=local" -Properties HomeDirectory | Where-Object {  $_.HomeDirectory -like "\\oldserver\user\*" } | set-aduser -HomeDirectory $($_.HomeDirectory -replace 'oldserver', 'newserver')

    But it doesn't. Errormessage is:

    set-aduser : replace
    In Zeile:3 Zeichen:1
    + set-aduser -HomeDirectory $($_.HomeDirectory -replace 'oldserver', 'newserver')
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (CN=someone...,DC=local:ADUser) [Set-ADUser], ADInvalidOperationException
    + FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.SetADUser

    All tipps from you are highly appreciated, since I have no clue what's wrong.

    Thank you very much!


  • #102086

    Olaf Soyk
    get-aduser -Filter { SamAccountName -like "seppl" } -SearchBase "OU=some, OU=entries, DC=here, DC=local" -Properties HomeDirectory | 
        Where-Object {  $_.HomeDirectory -like "\\oldserver\user\*" } |
                Set-ADUser -Identity $_.sAMAccountName -HomeDirectory $($_.HomeDirectory -replace 'oldserver', 'newserver')

    This way should work. If you have the sAMAccountNames given you don't need to search for the account first. Simply use a forach loop instead.
    BTW: Es gibt auch ein deutsches Powershell Forum bei Microsoft. Manchmal ist es einfacher, Fragen in seiner Muttersprache fragen zu können. 😉

  • #102091

    Alex Reil

    Hi Olaf,

    thank you very much, I was thinking of foreach before, but I thought it would not be needed. I thought using a pipe with "get-something | set-something" would do the same...

    Now it works, thanks for your help. Und ja, das deutsche Powershell Forum wäre praktischer 😉


You must be logged in to reply to this topic.