phpman13
phpman13

Reputation: 37

Creating a powershell array on the fly

I am creating a powershell script and need some help. I need to create an array and attach it to a string.

I have 3 required parameters

$printername
$start
$end

So if user enters the following for the 3 parameters: hp, 1, 5 I need to attach the following to a string called $printers $printers = hp1 hp2 hp3 hp4 hp5.

If they entered 1000 for the last parameter this would have to go through hp1000

How can I create this array.

Upvotes: 0

Views: 506

Answers (1)

mjolinor
mjolinor

Reputation: 68273

Seems simple enough.

function Make-PrinterString
{
   Param (
    [string]$Prefix,
    [int]$Start,
    [int]$End
    )

   [string]($start..$end | foreach {"$Prefix$_"})
}

Make-PrinterString HP 1 5

HP1 HP2 HP3 HP4 HP5

Upvotes: 3

Related Questions