r/PowerShell • u/SomeUTAUguy • Mar 18 '26
Question Can you build an array with a set number of spaces/slots?
Not really sure how to describe this but I am trying to build a script that helps with disabling users at work. When users are disabled the requestor can ask for IT to give proxy access to the users mailbox, onedrive, or both. The requester can also request that multiple people can have proxy access with each having different accesses. For instance one user can have both mailbox and onedrive while another has just mailbox and another has just onedrive.
My thought was to start by making a boolean question that asks the user "was proxy access requested?"
If yes it makes a variable called proxy access that is true. (I know I would call this variable for another step later on which is also why i made it)
Then the script would look at that variable and if true ask "How many proxy users?" The user would then type in a number and then it creates an array with that many spots/slots. (For instance if there are 3 proxy users the array has 3 slots). Once that is done the user types in each proxy user'd name and then when all of them are entered, the script then asks for each user in the array if they were granted mail box or onedrive access and depending on what the user puts the script would apply that to the user in the array.
The problem i am having is cant seem to find if there is a way to set up an array with a set linit or if i am over complicating this.
4
u/BlackV Mar 18 '26 edited Mar 18 '26
are you talking about a GUI?
why would you need to pre make 3 empty slots, why would you want to know how many proxies?
for a dirty example
I know its terrible example, but if you just entered
bobthen you would have 1 item,bob,jane, 2 items, enter nothing no proxiesor a simple loop that asks for input over and over till you enter an empty line (
do/while,do/until)I feel like you are complicating a thing that is just an input box
but really where is your input coming from, why is it manual ? why isn't this (for example) filled out in a Microsoft form, that gives you a CSV ? your fixed CSV then is used for your script, you can also then validate data based on that
1
u/az987654 Mar 18 '26
This entire ask is over complicated
2
u/frAgileIT Mar 18 '26
Just do a loop until the counter is zero and decrement it each pass. You could also not ask how many and just loop until the user says ‘end’ or enters a null or empty value that way you just keep asking until they’re done listing proxy users. Lots of ways to solve this one.
2
u/jimb2 Mar 18 '26
The design style of powershell is create as needed, so you don't usually need to pre-size. I might do something like this to create an array in steps:
Another single line way
1
u/Zozorak Mar 18 '26
Not sure I fully understand but there are a few ways to achieve what I think you want.
For loop
Do-while
Do while can be dangerous so probably not best practice. Also may have formatted wrong or buggered something up, am on mobile.
1
u/ankokudaishogun Mar 18 '26
I'll leave others to suggest better approaches, but the direct answer to your title question is
[array]::CreateInstance($ElementType, $Lenght)for example:
results into