how is it possible to enter a name automatically in a rename script?

Since you want it to print out the first 10 characters and only have to type 2, you could do a Write-Host followed by the Read-Host

Write-Host "    Current name: $($env:COMPUTERNAME)`n`n"

$lasttwo = Read-Host -Prompt '    Enter the last two characters of the new name'

$newname = $env:computername[0..9] + $lasttwo

$user = Read-Host -Prompt '    Input the username'

Rename-Computer -ComputerName localhost -NewName $newname -DomainCredential $user; Restart-Computer

Edit

Thanks to AdminOfThings for pointing out if the name is less than 10 characters the above approach would error. The following changes would prevent errors unless strictmode version 3.0 is set

Write-Host "    Current name: $($env:COMPUTERNAME)`n`n"

$lasttwo = Read-Host -Prompt '    Enter the last two characters of the new name'

$newname = $($env:COMPUTERNAME).Substring(0,10) + $lasttwo

$user = Read-Host -Prompt '    Input the username'

Rename-Computer -ComputerName localhost -NewName $newname -DomainCredential $user; Restart-Computer

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top