PowerShell:- Datatype – Array – Declaration & Initialization

Background

In this post let us consider how to declare, initialize, populate, and iterate an array.

Introduction

We have a few avenues for declaring an Array in PowerShell.

Our options are:-

  1. PowerShell
    • @()
      • $nameList = @();
    • String Array
      • [string[]]$nameList = $null;
  2. .Net
    • System.Collections.ArrayList
      • $nameList = [System.Collections.ArrayList]::new()

Script

Outline

  1. Array
    • Declare Array
      • Declare And Initialized An Array
        • [string[]]$nameListInitialized = $null;
      • Declare An Array
        • [string[]]$nameListInitialized;
    • Populate Array
      • $nameListInitialized += “Joya”;
        $nameListInitialized += “Tom”;
        $nameListInitialized += “Kate”;
    • Display Array Entry
      foreach ($name in $nameListInitialized)
      {
          Write-Host $name
      }
      

Code

<#
  Declare Variables
#>
[int] $i=0;
[int] $numberofEntries =0;
[string] $FORMAT_


<#
  Declare Variables
#>
[int] $i=0;
[int] $numberofEntries =0;
[string] $FORMAT_NAME_DETAIL="";
[string] $FORMAT_NAME_AGGREGATE="";
[string] $title="";

<#
    Declare strongly typed array of string 
#>
[string[]]$nameListInitialized = $null;
[string[]]$nameListUnInitialized;

<#
   Prepare Variables
#>
$FORMAT_NAME_DETAIL = "{0,3}) {1,-60}"
$FORMAT_NAME_AGGREGATE = "`tNumber of entries:- {0}"

<#
   Populate Array
#>
$nameListInitialized += "Joya";
$nameListInitialized += "Tom";
$nameListInitialized += "Kate";

<#
   Populate Array
#>
$nameListUnInitialized += "Joya";
$nameListUnInitialized += "Tom";
$nameListUnInitialized += "Kate";

$i =0;

$FORMAT_NAME_DETAIL = "{0,3}) {1,-60}"


Write-Host "";   

Write-Host "";   



$title = "List - `$nameListInitialized";

Write-Host $title;

Write-Host "";   

<#
    Iterate list of names
#>
foreach ($name in $nameListInitialized)
{
        
    $i = $i + 1;
        
    $log = $FORMAT_NAME_DETAIL -f $i, $name;
        
    Write-Host $log
        
    Write-Host "";   
}

$numberofEntries = $nameListInitialized.count;

$log = $FORMAT_NAME_AGGREGATE -f $numberofEntries;
    
Write-Host $log
    


<#
    Iterate list of names
#>

$i =0;

Write-Host "";   
Write-Host "";   


$title = "List - `$nameListUnInitialized";

Write-Host $title;

Write-Host "";   

foreach ($name in $nameListUnInitialized)
{
        
    $i = $i + 1;
        
    $log = $FORMAT_NAME_DETAIL -f $i, $name;
        
    Write-Host $log
        
    Write-Host "";   
}

$numberofEntries = $nameListUnInitialized.count;

$log = $FORMAT_NAME_AGGREGATE -f $numberofEntries;
    
Write-Host $log
    
Write-Host "";   

 

Output

 

Output - Text


List - $nameListInitialized

  1) Joya

  2) Tom

  3) Kate

        Number of entries:- 3


List - $nameListUnInitialized

  1) JoyaTomKate

        Number of entries:- 1

 

Output - Image

 

Source Code Control

GitLab

PowerShell - Array - Declaration & Initialization - Sample - nameList
Link

Bug Report

Microsoft

PowerShell

An Uninitialized declared array leads to string concatenation #17313
Link

One thought on “PowerShell:- Datatype – Array – Declaration & Initialization

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s