-1

I have a range of numbers (say, 20-100). Lets say the following are occupied: 20, 21, 22, 24. 25, 26, 28.

I ask the user how many free numbers he wants. Lets say, he says three numbers. So my output should be 23, 27, 29.

How can I quickly do this with powershell?

Thanks!

user1534235
  • 161
  • 3
  • 16

2 Answers2

8
$range = 20..100
$occupied = 20,21,22,24,25,26,28
$range | where-object {$occupied -notcontains $_} | `
select-object -first (read-host -Prompt "How many free numbers do you need?")
jon Z
  • 15,838
  • 1
  • 33
  • 35
1

You can also use the Compare-Object cmdlet:

compare $range $occupied -PassThru

23
27
29
30
31
...
100
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • I know this is soooo old but jon Z's answer is more accurate. Shay Levy's is more concise. Either way, this came in handy. – Jay Adams May 29 '19 at 01:26