Script to change computer name

Lisk Entaille 40 Reputation points
2024-05-27T17:15:45.0166667+00:00

Is there a script available that can change a computer's name? I need to rename hundreds of machines in my environment based on their region, building, and floor. Ideally, I would like to push the script using SCCM, but I have not had success with any of the methods I have tried so far.

Any assistance would be appreciated.

Microsoft Configuration Manager Deployment
Microsoft Configuration Manager Deployment
Microsoft Configuration Manager: An integrated solution for for managing large groups of personal computers and servers.Deployment: The process of delivering, assembling, and maintaining a particular version of a software system at a site.
926 questions
0 comments No comments
{count} votes

Accepted answer
  1. Marcin Policht 16,420 Reputation points MVP
    2024-05-27T21:11:32.3633333+00:00

    You can use a PowerShell script to rename computers based on their region, building, and floor. The script can be deployed via SCCM to rename multiple machines. Below is a PowerShell script that takes the region, building, and floor as parameters to generate a new computer name and rename the computer accordingly.

    This script can be adapted to run as a task sequence or package in SCCM.

    param (
        [Parameter(Mandatory=$true)]
        [string]$Region,
        [Parameter(Mandatory=$true)]
        [string]$Building,
        [Parameter(Mandatory=$true)]
        [string]$Floor
    )
    # Generate the new computer name
    $newComputerName = "${Region}-${Building}-${Floor}-$(Get-Random -Minimum 1000 -Maximum 9999)"
    # Get the current computer name
    $currentComputerName = (Get-WmiObject -Class Win32_ComputerSystem).Name
    # Rename the computer
    Rename-Computer -NewName $newComputerName -Force -PassThru
    # Restart the computer to apply changes
    Restart-Computer -Force
    
    

    Steps to Deploy via SCCM

    Save the Script: Save the PowerShell script to a file, for example, RenameComputers.ps1.

    Create a Package in SCCM:

    • Open the SCCM console.
      • Go to Software Library > Overview > Application Management > Packages.
        • Create a new package and provide details like name, source folder (where your script is saved), etc.
          • Add a program for the package, using powershell.exe -ExecutionPolicy Bypass -File RenameComputers.ps1 -Region <Region> -Building <Building> -Floor <Floor> as the command line.
          Distribute the Package:
          - Distribute the package to the desired distribution points.
          
          **Deploy the Package**:
          
             - Create a deployment for the package targeting the desired collection of computers.
          

    Specify Parameters:

    • If you're deploying to different sets of machines based on region, building, and floor, you might need multiple deployments with different parameters for each set.

    In the SCCM deployment wizard, when specifying the command line for the package, you can include the parameters as follows:

    powershell.exe -ExecutionPolicy Bypass -File RenameComputers.ps1 -Region "NA" -Building "B1" -Floor "F1"


    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin

    2 people found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful