#PowerShell script to generate a new Random Computer Name

Here is a #PowerShell script that generates a new Random Computer Name. The Function can be called with a parameter variable for Computer Name Length as an Integer.

Usage : New-RandomComputerName -NameLength 20

Function New-RandomComputerName
{
    [CmdletBinding(SupportsShouldProcess=$True)]

    Param(
        [int]$NameLength
    )

    #Characters Sets to be for Password Creation

    $CharSimple = "A","B","C","D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z","1","2","3","4","5","6","7","8","9","0"
    $CharNumbers = "1","2","3","4","5","6","7","8","9","0"
     
    #Verify if the Password contains at least 1 digit character

    $ContainsNumber = $False
    $Name = $Null
     
    #Sets which Character Array to use based on $Complex

    #Loop to actually generate the password

    for ($i=0;$i -lt $NameLength; $i++)
        {$c = Get-Random -InputObject $CharSimple
            if ([char]::IsDigit($c))
        {$ContainsNumber = $True}
         $Name += $c}
    
    #Check to see if a Digit was seen, if not, fixit

    if ($ContainsNumber)
        {
            Return $Name
        }
        else
        {
            $Position = Get-Random -Maximum $NameLength
            $Number = Get-Random -InputObject $CharNumbers
            $NameArray = $Name.ToCharArray()
            $NameArray[$Position] = $Number
            $Name = ""
            foreach ($s in $NameArray)
            {
                $Name += $s
            }
        Return $Name
       
    }
}

# Usage : New-RandomComputerName -NameLength 20

Cheers 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: