Bulk change HomeDirectory for AD-Accounts

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

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

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

Alex