Skip to content
Snippets Groups Projects

Rename-ADDomain

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Adphi
    Edited
    Rename-ADDomain.ps1 1.83 KiB
    function Rename-ADDomain {
        <#
        .SYNOPSIS
        Remame-ADDomain rename the active directory domain on all Domain Controllers
    
        .DESCRIPTION
        Remame-ADDomain rename the active directory domain on all Domain Controllers
    
        .ROLE
        Administrators
        #>
        param(
            [Parameter(Mandatory = $true)]
            [String]
            $NewDomain,
            [Parameter(Mandatory = $true)]
            [String]
            $NewNetBIOSName,
            [Switch]
            $Restart
        )
        $ErrorActionPreference = 'Stop'
        $ProgressPreference = 'SilentlyContinue'
    
        # Get domain name
        $oldDomain = (Get-ADDomain).Forest
        $oldName = (Get-ADDomain).NetBIOSName
    
        $tmpDir = "C:\Windows\Temp"
        $originalLocation = Get-Location
        Set-Location $tmpDir
        $listFile = "$tmpDir\Domainlist.xml"
        $listFileBkp = "$listFile.bkp"
    
        Write-Host "Dumping Domain listfile to $listFile"
        rendom /list /listfile:$listFile
        Write-Debug "Moving $listFile to $listFileBkp"
        Move-Item $listFile $listFileBkp -Force
        Write-Debug "Updating listfile $listFile"
        Get-Content $listFileBkp | ForEach-Object { $_ -Replace "$oldDomain", "$NewDomain" } | ForEach-Object { $_ -Replace "$oldName", "$NewNetBIOSName" } > $listFile
    
        Write-Host "New Domain"
        rendom /showforest /listfile:$listFile
        Write-Host "Uploading new Domain listfile"
        rendom /upload /listfile:$listFile | Out-Null
        Write-Host "Preparing to rename domain"
        rendom /prepare /listfile:$listFile | Out-Null
        Write-Host "Executing domain rename"
        rendom /execute /listfile:$listFile | Out-Null
    
        Write-Debug "Cleaning $listFile"
        Remove-Item -Force $listFile | Out-Null
        Write-Debug "Cleaning $listFileBkp"
        Remove-Item -Force $listFileBkp | Out-Null
    
        Set-Location $originalLocation
    
        if($Restart) {
            Write-Host "Restarting"
            Restart-Computer -Force
        }
    }
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment