none
Script to edit All UPN suffixes RRS feed

  • Question

  • Hi All,

    I'm struggling a bit with a script to bulk edit UPN suffixes.  I seem to be able to get one existing one (example @abc),but since others such as the default AD suffix such as abc.domain.com doesn't flip over to the xyz@com, so need something like a wildcard for that source domain, but no matter what I try, can't get it to work:

     Import-Module ActiveDirectory

    Get-ADUser -Filter {UserPrincipalName -like "*@abc.com"} -SearchBase "DC=abc,DC=child,DC=domain,DC=com"| 
    ForEach-Object {$UPN = $_.UserPrincipalName.Replace("abc.com","xyz.com")
        Set-ADUser $_ -UserPrincipalName $UPN
    }

    Thanks all for the kind assistance!

     
    Friday, September 30, 2016 2:36 PM

Answers

  • You have to do the update inside of the loop.  "Replace" does not append anything.

    Get-ADUser -Filter "UserPrincipalName -like '*abc.com'" -SearchBase "DC=prod,DC=ds,DC=abc,DC=com" | 
    	ForEach-Object {
    		Write-Host ('Before' +  $_.UserPrincipal)
    		$UPN = $_.UserPrincipalName.Replace('abc.com','xyz.com')
    		Write-Host "AFTER: $UPN"
    		Set-ADUser $_ -UserPrincipalName $UPN
    	}


    \_(ツ)_/

    • Marked as answer by djeinck Friday, September 30, 2016 8:36 PM
    Friday, September 30, 2016 6:57 PM

All replies

  • Hi,

    I'm not clear on what you're asking. Please clarify.


    Friday, September 30, 2016 2:43 PM
  • Hi Mike,

    Looking for some sort of some sort of txt manipulation to replace all UPN suffixes to a target suffix. So *.whateverSuffix -->@xyz.com for above FOR loop.

    thanks! 

    Friday, September 30, 2016 2:50 PM
  • 'abc.com' -Replace '...\.com','xyz.com'

    'def.com' -Replace '...\.com','xyz.com'

    'ghi.com' -Replace '...\.com','xyz.com'

    If number of characters varies

    'Vincent@contoso.com' -Replace('@.*\.com','@xyz.com')

    Friday, September 30, 2016 2:55 PM
  • PS D:\scripts> 'joe@abc.com' -replace '(.*)@.*', '1$@newdom.com'
    1$@newdom.com
    PS D:\scripts> 'joe@abc.com' -replace '(.*)@.*', '$1@newdom.com'
    joe@newdom.com
    PS D:\scripts> 'joe@xyz.abc.com' -replace '(.*)@.*', '$1@newdom.com'
    joe@newdom.com
    PS D:\scripts> 'joe@fff.xxx.com' -replace '(.*)@.*', '$1@newdom.com'
    joe@newdom.com
    PS D:\scripts>


    \_(ツ)_/

    Friday, September 30, 2016 3:43 PM
  • Above makes sense and think I'm close, but still appear to be a bonehead n00b and not working..in fact, it's throwing errors and no longer working now on any of the suffixes :-(

    Get-ADUser -Filter {UserPrincipalName -like '*@domain.com'} -SearchBase "DC=prod,DC=child,DC=domain,DC=com"| 
    ForEach-Object {$UPN = $_.UserPrincipalName.Replace('(.*)@.*','xyz.com')
        Set-ADUser $_ -UserPrincipalName $UPN
    }

    Friday, September 30, 2016 4:02 PM
  • Keep in mind that nobody can see your screen. So if you say there are errors, you have to copy and paste them. Otherwise, guessing games get boring pretty quickly.

    -- Bill Stewart [Bill_Stewart]

    Friday, September 30, 2016 5:50 PM
    Moderator
  • Sorry! Able to see, or too small?

    


    • Edited by djeinck Friday, September 30, 2016 5:59 PM
    Friday, September 30, 2016 5:58 PM
  • Please do not post images of code.  Use the code posting tool so that we can actually read what you poste.

    Copy and paste text of errors and not a blurred web image.

    I showed you an example of the code posting tool above.  Isn't it much easier to read and understand?


    \_(ツ)_/

    Friday, September 30, 2016 6:03 PM
  • You're missing the comma between the two.

    In the future please don't post screenshots. Use the 'Insert Code Block' button of the editor to post code and errors.


    Friday, September 30, 2016 6:03 PM
  • Import-Module ActiveDirectory
     
    Get-ADUser -Filter "UserPrincipalName -like '*abc.com'" -SearchBase "DC=prod,DC=ds,DC=abc,DC=com" | 
    ForEach-Object {$UPN = $_.UserPrincipalName.Replace('abc.com''xyz.com')
        Set-ADUser $_ -UserPrincipalName $UPN
    }
    
    
    Cannot find an overload for "Replace" and the argument count: "1".
    At C:\Users\p-deinck_lab\Desktop\UPNfrom.PS1:4 char:17
    + ForEach-Object {$UPN = $_.UserPrincipalName.Replace('abc.com','xyz.com'                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodException
        + FullyQualifiedErrorId : MethodCountCouldNotFindBest
     
    Cannot find an overload for "Replace" and the argument count: "1".
    At C:\Users\p-deinck_lab\Desktop\UPNFrom.PS1:4 char:17
    + ForEach-Object {$UPN = $_.UserPrincipalName.Replace('abc.com''xyz.com ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodException
        + FullyQualifiedErrorId : MethodCountCouldNotFindBest
    

    Apologies once more! Appreciate it guys, really do!


    Friday, September 30, 2016 6:22 PM
  • I just saw the reply come through that the comma is missing. added it .  Now no error, but its now appending, rather than replacing  my new suffix to say the old domain name. 

    Import-Module ActiveDirectory
     
    Get-ADUser -Filter "UserPrincipalName -like '*abc.com'" -SearchBase "DC=prod,DC=ds,DC=abc,DC=com" | 
    ForEach-Object {$UPN = $_.UserPrincipalName.Replace('abc.com','xyz.com')
        Set-ADUser $_ -UserPrincipalName $UPN

    Friday, September 30, 2016 6:23 PM
  • You have to do the update inside of the loop.  "Replace" does not append anything.

    Get-ADUser -Filter "UserPrincipalName -like '*abc.com'" -SearchBase "DC=prod,DC=ds,DC=abc,DC=com" | 
    	ForEach-Object {
    		Write-Host ('Before' +  $_.UserPrincipal)
    		$UPN = $_.UserPrincipalName.Replace('abc.com','xyz.com')
    		Write-Host "AFTER: $UPN"
    		Set-ADUser $_ -UserPrincipalName $UPN
    	}


    \_(ツ)_/

    • Marked as answer by djeinck Friday, September 30, 2016 8:36 PM
    Friday, September 30, 2016 6:57 PM
  • Wow, works finally  to my liking. Although much more painful for you guys & myself, I learned from it :-)

    Have a wonderful day!

    Friday, September 30, 2016 8:38 PM