0

How it is possible to create a object and numbering it?

I want to run a loop and create objects with different names.

$url = "http://stackoverflow.com"
$n = 30

for($i = 0 ; $i -le $n; $i++)
{
($ie +$i) = New-Object -ComObject InternetExplorer.Application
($ie +$i).visible = $true
($ie +$i).Navigate($url)
}

So that the results are the objects:

$ie1 $ie2 $ie3 etc.

1
  • 1
    As @JPBlanc said, you can use New-Variable, but why don't you create an array of objects? You could then access the individual objects with $ie[0], $ie[1], etc. This makes it easier to iterate through all of the objects (eg. to close all instances of Internet explorer: $ie| foreach { $_.Quit() }) Commented Oct 20, 2011 at 13:18

1 Answer 1

2

You can use New-Variable CmdLet to create a var in wich you compose the name dynamicaly.

PS C:\Temp> $a = "AV"
PS C:\Temp> $newV = New-Variable -Name ($a+"123") -Value (New-Object -ComObject InternetExplorer.Application)
PS C:\Temp> Get-Variable av123

Name                           Value
----                           -----
AV123                          System.__ComObject
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.