Bulk change HomeDirectory for AD-Accounts

Welcome Forums General PowerShell Q&A Bulk change HomeDirectory for AD-Accounts

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

 
Participant
6 months, 1 week ago.

  • Author
    Posts
  • #102085

    Participant
    Points: 1
    Rank: Member

    Hi,

    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!

    Alex

  • #102086

    Participant
    Points: 269
    Helping Hand
    Rank: Contributor
    get-aduser -Filter { SamAccountName -like "seppl" } -SearchBase "OU=some, OU=entries, DC=here, DC=local" -Properties HomeDirectory | 
        Where-Object {  $_.HomeDirectory -like "\\oldserver\user\*" } |
            ForEach-Object{
                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

    Participant
    Points: 1
    Rank: Member

    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 😉

    Alex

The topic ‘Bulk change HomeDirectory for AD-Accounts’ is closed to new replies.