.NET Framework - how to assign values to array and how to create array via variable

Asked By Fran
13-Mar-07 03:59 PM
Hi,

I need to assign values to an array, but they may not be in order. ie,

$ar[0] = "test"
but I get the error:

Array assignment failed because index '0' was out of range.
At line:1 char:5
+ $ar[0 <<<< ] = "test"

I can add them in sequence, via: $ar += "test" but I cannot add via specific
element.

Also, is there a way to create an array via a variable name, ie:

$var1 = "testname"

$(`$var1`) = @()

The goal is to create an array $testname.

Thanks in advance,

Frank
MyHash
(1)
VarArray
(1)
PowerShell
(1)
FrankRe
(1)
MSFTNGP
(1)
BZHHA
(1)
MSFT
(1)
TK
(1)
  Clint Bergman replied...
13-Mar-07 05:18 PM
You can create an array like this:
$varArray = "Value1","Value2","Value3","Value4","Value5"

and append values as you noted:
$varArray += "Value6"

I don't think you can just randomly assign a value to an arbitrary element.
Changing an arbitrary element is easy though.  So what I have done in the
past is something like this:

$Arr = 1..20
$Arr[2] = "Value1"
$Arr[6] = "Value6"
$Arr += 1..10

for more info:
get-help about_array

~Clint
  Lee Holmes [MSFT] replied...
23-Mar-07 04:37 PM
For your first question, will the array be full when you are done? If so,
Clint's response is correct.

If you don't plan to fill the array (i.e.: numbers are really just random,)
then a Hashtable is the best bet:

$myHash = @{}
$myHash[999] = "Test"
$myHash[999]

--
Lee Holmes [MSFT]
Windows PowerShell Development
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.
help
uninstalling Version 1.0 in anticipation of installing the CTA. I have no reference to Powershell or the referenced Updates when I run add / remove programs - with the show updates box Version 1 and see if the uninstall appears in add / remove.) Thank you, Fred Jacobowitz Powershell Discussions Microsoft.PowerShell.Commands.Management.resources (1) Microsoft.PowerShell.Commands.Utility.resources (1) Microsoft.PowerShell.ConsoleHost.resources (1) Microsoft.PowerShell.Commands.Management (1) Microsoft.PowerShell.Security.resources (1) Microsoft.PowerShell.ConsoleHost (1) System.Management.Automation.resources (1) Windows XP (1) In message <1194565205.518301.302920
Powershell omitted in Vista. .NET Framework Why was powershell omitted in the default installation of Windows Vista? Why didn't the powershell team think of using UAC to elevate any application using powershell scripts to do certain tasks that may be virulent? And may be to have a exclusion list requires elevated privileges through UAC. I do expect an answer for this in PowerShell in Action by Bruce Payette, but not sure whether the answer is there actually, as book. Please pour in your thoughts. Hope this will be a interesting discussion. . . Regards, Mugunth Powershell Discussions Powershell (1) Vista (1) IIS (1) MVP (1) XP (1) UAC (1) ScriptingAnswers (1) OPTIONAL (1) PowerShell wasn't "done" in time to be included in the final release of Vista. The
Passing arguments from .BAT to PowerShell script .NET Framework Hi, I'd like to have a PS script, where I can with spaces 2.txt script.ps1: foreach($arg in $args) { write-host "[$arg]" } .BAT file: powershell . \ script.ps1 %* If I drag and drop the files onto the batch file I get the following output: F: \ PowerShell> powershell . \ test.ps1 "F: \ PowerShell \ file with spaces 1.txt" "F PowerShell \ file with spaces 2.txt" [F: \ PowerShell \ file] [with] [spaces] [1.txt] [F: \ PowerShell \ file] [with] [spaces] [2.txt] That is clearly not what I want. And in my
PowerShell 2.0 CTP .NET Framework WS-Management v1.1 is a prerequisite to installation. Is this intentional. If so PowerShell is growing by 30-40%. It seems to me the functions of PowerShell should simply be limited by what is available rather than requiring various prerequisites. Dependencies are expensive for developers, testers, support and end users. Otherwise, both WS_M and Powershell installed smoothly. Most of my scripts still run (haven't checked them all). No complains so far. NICE WORK!! Ed Powershell Discussions DRMPOY1ekOMHFAUE (1) Windows Home Server (1) Windows XP (1) System.Net.HttpListener (1) Windows 1) Yes. It is needed for the remoting functionality as detailed in posts on the PowerShell Team blog - - Richard Siddaway Please note that all scripts are supplied "as is" and with no warranty Blog: http: / / richardsiddaway.spaces.live.com / PowerShell User Group: http: / / www.get-psuguk.org.uk It's also needed for the new gmail.com MVP - Admin Frameworks and Security I think the best situation would be that PowerShell always installs but, if you try to do something that requires WinRM, it fails with
General PowerShell Advice for Newbie .NET Framework Hi All, In the last 6-8 months I've some reasonable results - but all the time finding it pretty hard going. Then along comes PowerShell . . . . . WOW! This thing rocks! Suddenly I'm able to do things in 10 minutes of would have had me floored for hours! And I'm only on chapter three of "PowerShell in Action". What an awesome book! Q1: Is it worth, do you think me pursuing VBScript any further? Should I abandon further VBScript learning in favour of PowerShell? Q2: I am aware of Sapiens superb PrimalScript - but can't afford it. Can anyone recommend a good (cheap!) IDE for PowerShell? Q3: Is there an easy way to get user input in PowerShell using windows forms as the entry media? Just a push in the right direction is looking for. Thanks, Stuart - - Posted via a free Usenet account from http: / / www.teranews.com Powershell Discussions Windows XP (1) Vista (1) UNIX (1) PowerGUI (1) VBScript (1) NSBasic (1) PSPad be in a position of using VBScript at work then learn it otherwise concentrate on PowerShell Q2. I use notepad!! To be honest I haven't found an IDE for PowerShell