Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

Try the function Read-HostArray in the below article which provides better solution to directly paste the array value into a text box and pass it to the script.

http://powershelldude.blogspot.com/2015/08/read-hostarray-function-to-paste-multi.html

share|improve this answer
1  
Welcome to ServerFault.com! We tend to prefer that you copy and paste the relevant information from a linked post instead of just the link itself. Links die and then your answer, no matter how accurate and right, becomes useless. – GregL Aug 20 '15 at 17:01

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.