Rename multiple computers using a CSV file

I’m trying to use the below script to rename a group of computers on our domain. I can’t figure out what the “$Server” variable contains.

Script from TN

PS C:\>$a = Import-Csv ServerNames.csv -Header OldName, NewName
PS C:\>Foreach [ $Server in $a ] {Rename-Computer -ComputerName $Server.OldName -NewName $Server.NewName -DomainCredential Domain01\Admin01 -Force -Restart}

$server will contain an object that has an OldName property and a NewName property - e.g., one line from the CSV file. It looks to me as if you’re using it correctly.

Thanks for your response Don! I understand now. $server is specifying what parameter to use on each Header in the csv file.

Mmm, well, let’s make sure the terminology is right ;).

Import-CSV produces a collection of objects. Each object represents one data row from the CSV. The objects have properties that correspond to the CSV’s header row.

$rows = Import-CSV something.csv

$rows will contain all the objects

foreach ($row in $rows) {
  $row.myproperty
}

Goes through each object in $rows, placing one object at a time into $row. $row.myproperty accesses the value in the “MyProperty” column of the current CSV data row. In your example, those property values are being passed to the -ComputerName and -NewName parameters of the command you’re running. So, $server.oldname refers to “the OldName column of the current CSV data row.” $server represents a single object, and OldName has become a property of that object.

I’m a bit of a stickler for terminology, because things get confusing otherwise. Commands have parameters; objects have properties. Parameters are a way of feeding input to a command; properties contain values.

Thanks for your thorough explanation! greatly appreciated and completely understood. thou shall use correct terms for future ref :slight_smile:

For anyone interested in easily renaming multiple computers using a CSV file without having to understand or learn powershell it may be worthwhile giving Netdom Rename Computer GUI a try. Although it doesn’t use powershell, Netdom can still get the job done and we have made it even more simple with a graphical user interface. All you need to do is import your CSV file, input your domain credentials and click Rename.

Get Netdom Rename Computer GUI

Bonjour,

Quelqu’un serait-il au courant si il est possible de renommer des machines qui sont deja intégré au domaine , sans devoir les enlever du domaine…

J’ai toujours l’erreur “Accès refusé”
Voici le code :

Fonction renmame permet de renommer les postes

function RenameDomaine ($tabArgument)
{

$adminlogin = "domaine\administrateur"
Read-Host -prompt "pwd admin du domaine " -AsSecureString | 
ConvertFrom-SecureString | out-file Monpwdadmin.pwd
$adminpassword = Cat .\Monpwdadmin.pwd | ConvertTo-SecureString
$adminCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminlogin, $adminpassword



if ($adobjSalle -ne $null)	
{
$confirmation = Read-Host "Etes-vous bien sur de vouloir renommer la salle $salle y/n ?"

	if ($confirmation -eq 'y') 
	{				
		foreach ($arg in $tabArgument)
		{			
			foreach($entry in $list) 
			{				
				if ($arg  -eq 'all' -or ($arg -eq $($entry.IP)))
				{							
					write-host "Traitement du poste: " $($entry.Nom) -foregroundcolor green
					$result = Get-WmiObject Win32_PingStatus -filter "address='$($entry.IP)'"
					if ($result.statuscode -eq 0)
					{							
						$newname2 = $($entry.Nom2)
						$newname = $($entry.Nom)
						Invoke-Command -computername $($entry.IP) -credential $admincredential -Scriptblock {param($newname2);rename-computer -NewName ($newname2) -force -restart}  -argumentlist $newname2,$admincredential -ErrorVariable error 
						
						if ($error -ne 0)
						{
						write-host "Rename echoue - voir detail sur log.txt"  -foregroundcolor red
						Add-Content -Path $event -Value "Rename KO -- $($entry.IP) $error "  	
						}
						else
						{
						write-host "Rename Ok" -foregroundcolor green
						
						}
					}	
					else
					{						
					write-host "Operation annulee verifier la connexion reseau" -foregroundcolor red
					}	
				}					
			}	
		}
	}
}
else
{
	Write-host " Operation annulee Verifier l'orthographe de la salle $salle" -foregroundcolor green
}	

}