Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I wanna create a powershell script in which I want to add multiple values to an Array (Add into Array).

eg: I have 10 users in my company and want to create their AD account (without using csv file), if I use "Read-Host" as below. Can I loop "read-host" to show 10 times and add values to an array and list as below??

$FirstName = Read-Host "Enter the first name of the employee"
$LastName = Read-Host "Enter the last name of the employee"
[INT]$empid = Read-Host "Enter the employee number"
$group = Read-Host "Enter the group name"
$homedrive = Read-Host "Enter the home drive"

$NewHire = @{}
$NewHire.Name = $FirstName
$NewHire.Empid = $empid
$NewHire.LastName = $LastName
$NewHire.homedrive = $homedrive

$Objectname = New-Object PSobject -Property $NewHire

$objectname

Output of this file would be as below,

Name  LastName Empid homedrive
----  -------- ----- ---------
phani ukkalam   3333 FDS      
share|improve this question

2 Answers 2

You can add a wscript shell to prompt the user for whether to continue or not, and put it in a do while loop.

The loop condition here, is dependant on the user pressing yes to add more users:

$wsh = new-object -comobject wscript.shell

do {
    $FirstName = Read-Host "Employee name"

    #and so on, and so on, end with this:

    $answer = $wsh.popup("Do you want to add more users?", 0,"More users?",4) 
    If ($answer -eq 6) { 
            $continue = $True 
        } else { 
            $continue = $False 
        } 
} while ($continue -eq $True) 
share|improve this answer
    
@Judaslscariot ... Thanks for the help. I clubbed both of your answers. –  Phanindran Dec 28 '11 at 5:53
    
@Phanindran ... you're welcome, no problem. Please be sure to accept an answer so the question doesn't figure as "Unanswered" –  Mathias R. Jessen Dec 28 '11 at 14:55

Yes. Here's an example.

$userCount = 10
$users = @()

1..$userCount | ForEach-Object {
    $FirstName = Read-Host "Enter the first name of the employee"
    $LastName = Read-Host "Enter the last name of the employee"
    [INT]$empid = Read-Host "Enter the employee number"
    $group = Read-Host "Enter the group name"
    $homedrive = Read-Host "Enter the home drive"

    $NewHire = @{}
    $NewHire.Name = $FirstName
    $NewHire.Empid = $empid
    $NewHire.LastName = $LastName
    $NewHire.homedrive = $homedrive

    $Objectname = New-Object PSobject -Property $NewHire

    $users += $Objectname
}

$users
share|improve this answer
    
@Andy... Thanks for the help. I clubbed both of your answers. –  Phanindran Dec 28 '11 at 5:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.