none
Remove character in computer name via Script RRS feed

  • Question

  • All:

    First, thanks for making time to read this.  I appreciate.  

    I have a customer that has some 80 computes, and all of them have a "-" in the computer name.  Like PC-101 or PC-102 and so forth.

    They are getting some new software on their servers that does not work well with the dash.  This software requires ONLY numbers or letters in the computer namespace.  So, basically, I need to rename 80 computers from PC-101 to PC101, etc.

    I tried this via Powershell:

    $NewName = $env:computername -replace "-" ""; rename-computer $NewName; restart-computer

    Which returned this: 

    rename-computer : Skip computer 'PC-100' with new name 'PC-100' because the new name is the same as the current
    name.
    At line:1 char:46
    + ...  = $env:computername -replace "-"""; rename-computer $NewName; restar ...
    +                                          ~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (COG-PC100:String) [Rename-Computer], InvalidOperationException
        + FullyQualifiedErrorId : NewNameIsOldName,Microsoft.PowerShell.Commands.RenameComputerCommand


    Any suggestions are very much appreciated.  I really don't want to have to do this manually.  

    Best,

    Jeff

    Monday, July 29, 2019 10:38 PM

All replies

  • First "replace uses RegEx. You need to write a correct expression.

    $name = 'PC-101'
    if ($name -match '-') {
        $newname = $name -replace '-'
        # Rename-Computer ...
    }
    


    \_(ツ)_/

    Monday, July 29, 2019 10:45 PM