somehume
somehume

Reputation: 154

How to remotely rename a list of computers via PowerShell?

I have a largish set of Windows 10 workstations that need to be renamed. I've tried running the script below, but get errors that are beyond my current PS level.

$computers = Import-Csv "c:\rename-computers\computers.csv" 
foreach ($oldname in $computers){
    #Write-Host "EmpID=" + $computers.NewName
    Rename-Computer -ComputerName $computers.OldName -NewName $computers.NewName -DomainCredential hole\inwall -Force -Restart
}

Produces:

Rename-Computer : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ComputerName'. Specified method is not supported. At \siat-ds0\appdeploy\LabPacks\rename-computers\rename-siat.ps1:4 char:35 + Rename-Computer -ComputerName $computers.OldName -NewName $computers.NewName ... + ~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Rename-Computer], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.RenameComputerCommand

I've seen similar closed threads on this topic elsewhere without mention of the error I'm receiving.

Upvotes: 0

Views: 2363

Answers (3)

pedja
pedja

Reputation: 1

Bulk rename computers in AD Powershell bulk rename computers in AD with test if pc is online and if new name is already taken and log "not-renamed" PC.

adc.csv 
oldname,newname 
WEDSKS0022,RKVKS0110 
WEDSKS0117,RKVKS1413
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force;
$computers = import-csv -Path ".\adc.csv"
$Credential = Get-Credential
$nisuprosli=".\notrenamed $(get-date -f dd-MM-yyyy-HHMM).csv"
$makecsv="oldname,newname" | Out-File $nisuprosli -Encoding utf8 -Append

foreach ($pc in $computers){
   
 $IsOldPCalive=Test-Connection -ComputerName $pc.oldname -Quiet -Count 1 -ErrorAction SilentlyContinue
 $IsNewPCalive=Test-Connection -ComputerName $pc.newname -Quiet -Count 1 -ErrorAction SilentlyContinue

 if ($IsOldPCalive -eq $True -and $IsNewPCalive -eq $False) {
      write-host "Rename PC $($pc.oldname) u $($pc.newname)" -ForegroundColor Cyan
      Rename-computer -computername $pc.oldname -newname $pc.newname -domaincredential $Credential -PassThru -force -restart #-WhatIf   
         }
    else {
    write-host "PC $($pc.oldname) is not available or already exists $($pc.newname)" -ForegroundColor Yellow
    $makecsv="$($pc.oldname),$($pc.newname)" | Out-File $nisuprosli -Encoding utf8 -Append
    }

}

Upvotes: 0

Ranadip Dutta
Ranadip Dutta

Reputation: 9133

You are iterating but not using the singular:

Instead of this:

foreach ($oldname in $computers){
    #Write-Host "EmpID=" + $computers.NewName
    Rename-Computer -ComputerName $computers.OldName -NewName $computers.NewName -DomainCredential hole\inwall -Force -Restart
}

Try this:

foreach ($oldname in $computers){
    Rename-Computer -ComputerName $oldname.OldName -NewName $oldname.NewName -DomainCredential hole\inwall -Force -Restart
}

Note: $oldname is holding one value at a point. So the number of computers present in $computers will come one by one to $oldname and will perform the activity inside the loop.

You should use the singular $oldname inside the loop to iterate one by one.

Upvotes: 1

mklement0
mklement0

Reputation: 437353

You mistakenly used the collection variable $computers instead of the loop-iteration variable $oldname inside your loop, and since $computers.NewName expanded to an array of names rather than a single one, you got the error you saw.

That said, you don't need a loop at all - a single pipeline will do:

Import-Csv "c:\rename-computers\computers.csv" |
 Rename-Computer -ComputerName { $_.OldName } -DomainCredential hole\inwall -Force -Restart

Rename-Computer will implicitly bind the NewName property of each input object to the -NewName parameter.

The -ComputerName parameter, by contrast, must be told what property on the input objects to access, given that the input objects have no ComputerName property.
This is what script block { $_.OldName } does, inside which automatic variable $_ represents the input object at hand.

To see which parameters accept pipeline input, examine the output from
Get-Help -full Rename-Computer; for details and a programmatic alternative, see this answer of mine.

Upvotes: 4

Related Questions