I'm new to Powershell so don't know a huge amount.

I have a menu and I have 2 options ("Add IP" and "List IP's")

The "Add IP" code is:

$UIP = Read-Host "Enter IP to whitelist"
$whitelist += $UIP

The "List IP" code is:

Write-Host "==== $Title Whitelist IPs ===="
Write-Host $whitelist

The only problem is that when I list the array it lists everything on the same line like this:

when I would like to to display like this:
  • You could use foreach and print one element at a time. – Gerard H. Pille Nov 3 '18 at 19:01
  • Did you define $whitelist as an array? See this StackOverflow answer which seems relevant : stackoverflow.com/questions/44676960/… – lx07 Nov 3 '18 at 19:31
  • Perfect! Thanks @lx07 that gave me the answer, correct code is: [array]$whitelist += $UIP – Sam.92 Nov 3 '18 at 20:10
  • @Sam.92 Please post that as an answer. – Twisty Impersonator Nov 4 '18 at 0:50
  • Read PowerShell back to basics: the arrays article. Predeclare an array as $whitelist = New-Object -typeName System.Collections.ArrayList or briefly as $whitelist = @(); then, apply Write-Host $whitelist -Separator ([environment]::NewLine). – JosefZ Nov 4 '18 at 17:40

You can user the for loop for to achieve this

for ($loopindex = 0; $loopindex -lt $whitelist.Count; $loopindex ++) { $whitelist[$loopindex] }


You do not have to use a loop to perfom this, if you do not want to perform any operations on each IP.

Simply use

Write-Host ($whitelist -join "`n")

This will inject a new line between each element in the array. I have compared the differences below:

$array = @('foo','bar')
write-host ($array -join "`n") 



$array = @('foo','bar')
write-host $array

foo bar

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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