The name says it all:



    Rename a computer.


    Rename a local or domain computer.


    No inputs.


    Outputs contents of self-generated $logfile. Used to capture verbose of script in logs.


    .\rename-computer -domain $domain -domainAC $domainAC -domainPass $pass -newComputerName $name -restart $restart.


    .\rename-computer -newComputerName $name -restart $restart.



$root = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent

#Set logfile.
$logFile = $root + "\Logs\$env:COMPUTERNAME-" + $MyInvocation.MyCommand.Name + $date + $logExtension

#Set execution policy.
(Set-ExecutionPolicy Bypass -Scope Process -Force -Confirm:$false) *>> $logFile

#Get domain membership first.
$onADomain = (Get-WmiObject -Class Win32_ComputerSystem).PartOfDomain

#If on domain procure domain credentials.
if ($onADomain) {

    #Check workgroup.
    $workgroup = ((Get-WmiObject -Class Win32_ComputerSystem).Workgroup) *>> $logFile
    ("Your computer is on domain $workgroup. You need to enter domain AC credentials...")  *>> $logFile

    #Prompt user for input if parameters were not passed as part of the script call.
    if (!$domain) {
        $domain = Read-Host "Enter AC domain e.g: my_domain"
    if (!$domainAC) {
        $domainAC = Read-Host "Enter domain AC admin username, e.g: admin"
    if (!$domainPass) {
        $domainPass = Read-Host "Enter domain AC admin password, e.g: ******" -AsSecureString        



#Prompt user for newcomputer name if not passed.
if (!$newComputerName) {
    $newComputerName = Read-Host "Enter new computer name, e.g: MY-NEW-COMPUTER1"

#Prompt user for restart if restart parameter not passed.
if (!$restart) {
    $restart = Read-Host "Do you wish to restart this computer? Y or N"

#If on domain execute Rename Computer.
if ($onADomain) {
    $credential = New-Object System.Management.Automation.PSCredential("$domain\$domainAC", $domainPass)
    (Rename-Computer -NewName $newComputerName -DomainCredential $credential -Force -Verbose -Confirm:$false) *>> $logFile
else {
    #Otherwise it's a local rename. 
    (Rename-Computer -NewName $newComputerName -Force -Verbose -Confirm:$false) *>> $logFile

#If restart parameter is passed. Computer will restart after a 5 sec buffer.
if ($restart.ToLower() -eq 'Y'.ToLower()) {
    ("Restarting...") *>> $logFile
    Start-Sleep -Seconds 5
    Restart-Computer -Force

#Output redirected verbose from logfile.
Get-Content -Path $logFile
Prevent data duplication in Excel VBA Auto-Update your framework

No Comments

No comments yet

Leave a Reply

Your email address will not be published. Required fields are marked *