3 mins read

Generate a List Of Random Names And Phone Numbers Using PowerShell!

currently creating a list of random names and phone numbers is just a matter of steps away.

150x150xJJAY45153PM8272014.jpeg.pagespeed.ic.6GIZ71KsMLYou can generate a list of random names and phone numbers using PowerShell, This list can actually represent Personally Identifiable Information or PII and you don’t even want to use any real data about real people. There are some certain tools which you need to experience this process:

 Powershell

 You can develop the script using PowerShel 4.0 and PowerShell 2.0. PowerShell 2.0 is compatible with Windows 7 and it also comes with Windows XP and Vista. PowerShell 4.0 comes with Windows 8.1 and it can be upgraded by Windows 7 SP1 users. This version is not available for XP or Vista. To get the event log you can use Get-EventLog Windows PowerShell cmdlet. When the Windows PowerShell engine transtions are running Windows PowerShell event 400 gets generated. Check out the following example:

 Prepare names

 You need a list of names for the random generator. First get lot of names and feed the generator so that lots of names and numbers get created at once. Each name and number pair takes around 1.5 seconds but the speed depends on your system specifications. Get the surnames, male names and female names. You just need to generate three text files which can be used as pools by the script. Store the files in the same folder where your PowerShell script is kept.

 Phone Number 

Just make sure that the phone number doesn’t match any person’s real phone number and to know the same use the most popular “555” Exchange Code. You can also make things more interesting by generating other phone numbers. Remember to give actual area codes and assigned area codes before every number. Research thoroughly and verify the rules and regulations before generating any phone number.

 The most used commands

 The most common commands for the operation are ForEach-Object, if…else statements, switch statements, while statements, try…catch statements, Get-Content, Write-Host and Write-Output. All these commands are for specified functions, preffered statements, repeating script block, error handling, presenting messages, putting things in the console and finally generating output.

 Now you have all the ingredients to build your script as followed:

 Start your script in a clean console by Clear-Host. Check the script and fill the positions like $ScriptFolder = Split-Path $MyInvocation.MyCommand.Definition -Parent$RequiredFiles = (‘Males.txt’,’Females.txt’,’Surnames.txt’). The first line is very crucial for any script and the second line creates a series of file names to run the script smoothly. This line sends the $RequiredFiles into a ForEch-Object block and uses the Test-Path. Note that you use double quotes and not single quotes. If any file is not found by PowerShell then an error message is posted to notify which file is missing. If actually $MissingFiles exists then Write-Host will again post a message telling you how many files are missing. Then all the variables will be cleaned up and the script will end.

 If everything falls in place, then the script will go on smoothly. Make an alias like New-Alias g Get-Random for creating alternate names for commands. If a list of names is provided then a random item is picked up. Random numbers can also be generated through this Get-Random.

 A script which creates one random name and number is great but if the user gets a choice of how many he/she can create in one batch then it’s better. The while statement for this is:

 while (!$ValidInput)

{

try

{

[int]$UserInput = Read-Host -Prompt ‘Items to be generated’

$ValidInput = $true

}

catch

{

Write-Host ‘Invalid input. Enter a number only.’ -ForegroundColor Red

}

}

 This statement checks and also negates the value of $ValidInput. Once the user gives a proper number as input the script is required to announce like Write-Host “`nGenerating $UserInput names & phone numbers. Please be patient.`n” 1..$UserInput | ForEach-Object { <# INSERT RANDOM NAME & NUMBER GENERATOR HERE #>}

 Then you come to the simplest part of the entire process, generating the name. Pick a surname, pick a gender and pick a first name. Now use it like:

 $Surname = Get-Content “$ScriptFolder\Surnames.txt” | g

 $Male = g 2

 if ($Male)

{$FirstName = Get-Content “$ScriptFolder\Males.txt” | g}

 else

{$FirstName = Get-Content “$ScriptFolder\Females.txt” | g}

 Now create a random phone number like:

 $NumberFormat = g 5

 switch ($NumberFormat)

{

0 {$Prefix = “($(g 2)$(g 10)$(g 10)) $(g 10)$(g 10)$(g 10)”}

1 {$Prefix = “($(g 10)9$(g 10)) $(g 10)$(g 10)$(g 10)”}

2 {$Prefix = “($(g 10)$(g 10)$(g 10)) $(g 2)$(g 10)$(g 10)”}

3 {$Prefix = “($(g 10)$(g 10)$(g 10)) $(g 10)11”}

4 {$Prefix = “($(g 10)$(g 10)$(g 10)) 555”}

}

 The first line signifies thr format of the number. Then a random choice is done. If you use double-quotes then you can also process script blocks. Before getting the output just generate a subscriber ID as:

 switch ($NumberFormat)

{

{$_ -lt 4} {$Suffix = “$(g 10)$(g 10)$(g 10)$(g 10)”}

4 {

switch ($Prefix)

{

‘(800) 555’ {$Suffix = ‘0199’}

default {$Suffix = “01$(g 10)$(g 10)”}

}

}

}

 Now, it’s time for output.

Write-Output “$FirstName $Surname $Prefix-$Suffix”

}

 Finally your job is done. Then it’s time for clean-up. The clean-up process goes as:

 Remove-Item alias:\g

Remove-Variable ScriptFolder,RequiredFiles,Surname,Male,FirstName,NumberFormat,Prefix,Suffix,ValidInput,UserInput

Leave a Reply