Where Learning is Pleasure

Jayaram Krishnaswamy

Subscribe to Jayaram Krishnaswamy: eMailAlertsEmail Alerts
Get Jayaram Krishnaswamy: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


Blog Feed Post

Handling Array(s) with Windows Power Shell

It is the same as a .NET array. In .NET System.Array is based on System.Object. Working with arrays in Power Shell is somewhat easier than in C#.

Assigning as Array. $ stands for a variable.


You assign the array to a variable 'flowers' as shown above. When you call the variable the array will be printed in the console as shown above.

This is same written explicitly as follows:

[email protected]("rose", "jasmine", "lily") 
Note array elements need not be of the same type.

Writing array content to console
You can also print to the console as shown in the next. $_ is a dynamic variable storing current information.


Write-Host $_:    write current information of each element you find to the console
|                      :   Pipe command. Get the command output and pass it to another

Finding its Type
The next code shows the type of 'flowers' as an Object.


Finding an array element by its index


Add another array item to the array. 'Sun Flower' is added. Adding element using the += operator is slow, using the System.Collections.ArrayList as a New Object is faster for additions. 


Find the last elements(array items) of the array. It is zero based. 
[ .. ] is called a range operator.


Reversing an array (last becomes first and first becomes last). :: stands for static member of a class


Start with an empty array and add elements to it. @ is array operator and @() is an empty array.


Searching with wild cards in an array (in names above)

Note:
*a finds two items
*ar finds only one

Create a 2D array.

Find the second element of the second array in the above


Add a new item to the second array




Read the original blog entry...

More Stories By Jayaram Krishnaswamy

Jayaram Krishnaswamy is a technical writer, mostly writing articles that are related to the web and databases. He is the author of SQL Server Integration Services published by Packt Publishers in the UK. His book, 'Learn SQL Server Reporting Services 2008' was also published by Packt Publishers Inc, Birmingham. 3. "Microsoft SQL Azure Enterprise Application Development" (Dec 2010) was published by Packt Publishing Inc. 4. "Microsoft Visual Studio LightSwitch Business Application Development [Paperback] "(2011) was published by Packt Publishing Inc. 5. "Learning SQL Server Reporting Services 2012 [Paperback]" (June 2013) was Published by Packt Publishing Inc. Visit his blogs at: http://hodentek.blogspot.com http://hodentekHelp.blogspot.com http://hodnetekMSSS.blogspot.com http://hodnetekMobile.blogspot.com He writes articles on several topics to many sites.