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      
link|improve this question
feedback

2 Answers

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) 
link|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
feedback

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
link|improve this answer
@Andy... Thanks for the help. I clubbed both of your answers. – Phanindran Dec 28 '11 at 5:52
feedback

Your Answer

 
or
required, but never shown

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