I'm working on a script aimed at helping with user management at my workplace, specifically for disabling users. When a user is disabled, the requester can ask IT for proxy access to the user's mailbox, OneDrive, or both. The requester might need proxy access for multiple users, each requiring different permissions. For example, one user might need both mailbox and OneDrive access, while another only needs mailbox access.
I thought about starting with a boolean prompt that asks, "Was proxy access requested?" If the answer is yes, I'd create a variable named `proxyAccess` set to true. Then, I'd want the script to ask, "How many proxy users do you need?" The idea is to create an array that has the number of slots equal to the number of users specified. So, if the user enters 3, the array would have 3 slots. After that, the users would input each proxy user's name, and the script would ask for any mailbox or OneDrive access for each user. However, I'm stuck on how to create an array of a specified size, or if I'm overcomplicating things. What's the best way to manage this?
5 Answers
You really don’t need to overthink this! The straightforward way in Powershell is to use the following method to create a resizable array:
```
$userList = @()
while ($true) {
$username = Read-Host 'Enter username (or press Enter to finish)'
if ($username -eq '') { break }
$userList += $username
}
```
This continuously adds to the array until the user indicates they are done.
If you’re looking to create an array in Powershell dynamically, you have a few options. You can use a simple loop to gather user input like this:
```
$userList = @()
do {
$username = Read-Host 'Enter a proxy username (leave blank to finish)'
if ($username) { $userList += $username }
} while ($username)
```
This approach allows you to build your array as users are added without needing to define its size upfront!
Are you sure you need to pre-define the array size? Instead of asking how many proxies upfront, you can just collect user input in a loop until they indicate they’re done. For example, you could read a comma-separated list of user names all at once, like this:
```
$users = Read-Host -Prompt 'Enter Proxy Users'
$proxyArray = $users -split ','
```
This way, you don’t have to deal with empty slots. Just keep gathering input as needed, and it simplifies the process!
You can definitely handle this with a simple loop. Rather than asking for a fixed number, just keep prompting the user for a username until they tell you they're finished by typing 'end' or leaving it blank. This way, you can keep adding proxy users without worrying about pre-setting the number of slots in your array!
For your original inquiry about creating an array with a fixed size, you can use:`[array]::CreateInstance($ElementType, $Length)`, but for most use cases in Powershell, you won’t need to pre-size at all. Just use the array constructor directly like this:
```
$a = [string[]]::new(10)
```
However, it might be easier to build the array dynamically based on user input as mentioned by others here!

Good point! Using array constructors or similar can be helpful, but flexibility with input often is the way to go in Powershell. Being able to account for any number of users dynamically is generally the preferred method!