WEBSITE

PowerShell for Microsoft SharePoint 2010 : Variables, Arrays, and Hashtables - Arrays in Windows PowerShell

2/24/2014 12:55:55 AM

An array is a container that is used for storing a collection of data elements. An array in Windows PowerShell can contain objects of all types supported by .NET. All arrays are origin zero, meaning that the first element in an array is always at index 0, the second element is at index 1, and so on. Many cmdlets in Windows PowerShell return output in the form of an array.

Here is an example of creating a simple array containing numeric values:

PS > $array = 1,2,3
PS > $array
1
2
3

You can also create an array using the array subexpression operator @, as shown here:

PS > $array = @(1,2,3)

You can access specific elements in arrays. To retrieve the first element, type the following:

PS > $array[0]
1

Notice how we use the value 0 to retrieve the first element, since the indexing of array elements in Windows PowerShell starts with zero. If we used 1, the second element would be returned.

You can change an element in an array by assigning it a new value, as in this example:

PS > $array[1] = "Two"
PS > $array
1
Two
3

Use the += operator to add an element to an array.

PS > $array += "Four"
PS > $array
1
Two
3
Four

You can also count the number of elements in an array by using the Count property.

PS > $array.Count
4

Note

The Count property used in this example is actually an alias for the System.Array Length property. It is available through the types.ps1.xml file, which is a built-in XML file that adds several elements to the .NET Framework types in Windows PowerShell.


When cmdlets return more than one result, Windows PowerShell automatically wraps the result into an array. In the following example, we store the output of the Get-SPWebTemplate cmdlet in a variable and use the Count property to check how many elements the array contains.

PS > $SPWebTemplate = Get-SPWebTemplate
PS > $SPWebTemplate.Count
50

Again, if we want to retrieve the first element, we can simply type the following:

PS > $SPWebTemplate[0]

Name Title LocaleId Custom
---- ----- -------- ------
GLOBAL#0 Global template 1033 False

You can also use ranges when retrieving elements in an array.

PS > $SPWebTemplate[0..2]

Name Title LocaleId Custom
---- ----- -------- ------
GLOBAL#0 Global template 1033 False
STS#0 Team Site 1033 False
STS#1 Blank Site 1033 False

It is even possible to use negative numbers when working with arrays. To retrieve the last element in the array, use -1.

PS > $SPWebTemplate[-1]

Name Title LocaleId Custom
---- ----- -------- ------
visprus#0 Visio Process Repository 1033 False


Other  
  •  Sharepoint 2010 : Administering Enterprise Content Management - Document Management (part 12) - Digital Asset Management
  •  Sharepoint 2010 : Administering Enterprise Content Management - Document Management (part 11) - eDiscovery and Hold , Retention
  •  Sharepoint 2010 : Administering Enterprise Content Management - Document Management (part 10) - In-Place Records Management
  •  Sharepoint 2010 : Administering Enterprise Content Management - Document Management (part 9) - Improved Records Center Site
  •  Sharepoint 2010 : Administering Enterprise Content Management - Document Management (part 8) - Content Type Syndication
  •  Sharepoint 2010 : Administering Enterprise Content Management - Document Management (part 7) - Term Store and Term Sets
  •  Sharepoint 2010 : Administering Enterprise Content Management - Document Management (part 6) - Managed Metadata Fields
  •  Sharepoint 2010 : Administering Enterprise Content Management - Document Management (part 5) - Content Types and Columns
  •  Sharepoint 2010 : Administering Enterprise Content Management - Document Management (part 4) - Document Sets , Location-Based Metadata Default Values
  •  Sharepoint 2010 : Administering Enterprise Content Management - Document Management (part 3) - Metadata Navigation and Filtering, The Content Organizer
  •  
    Video
    Top 10
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    The latest Audi TT : New angles for TT
    Era of million-dollar luxury cars
    Game Review : Hearthstone - Blackrock Mountain
    Game Review : Battlefield Hardline
    Google Chromecast
    Keyboards for Apple iPad Air 2 (part 3) - Logitech Ultrathin Keyboard Cover for iPad Air 2
    Keyboards for Apple iPad Air 2 (part 2) - Zagg Slim Book for iPad Air 2
    Keyboards for Apple iPad Air 2 (part 1) - Belkin Qode Ultimate Pro Keyboard Case for iPad Air 2
    Michael Kors Designs Stylish Tech Products for Women
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    Popular Tags
    Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Exchange Server Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe Photoshop CorelDRAW X5 CorelDraw 10 windows Phone 7 windows Phone 8 Iphone