# Display-Array1.ps1
# Sample using PowerShell
# Thomas Lee
# Helper function
function PrintValues{
param ($myArr )
foreach ($i in $myArr ) {
"`t{0}" -f $i
}
""
}
# Start of Sample
# Creates and initializes a new integer array and a new Object array.
$myIntArray = 1, 2, 3, 4, 5
[system.object] $myObjArray= [object] 26, [object] 27, [object] 28,[object] 29,[object] 30
# Prints the initial values of both arrays.
"Initially,"
"Integer array:"
PrintValues ( $myIntArray )
"Object array: "
PrintValues ( $myObjArray )
# Copies the first two elements from the integer array to the Object array.
[System.Array]::Copy( $myIntArray, $myObjArray, 2)
# Prints the values of the modified arrays.
"`nAfter copying the first two elements of the integer array to the Object array,"
"Integer array:"
PrintValues ( $myIntArray );
"Object array: "
PrintValues ( $myObjArray );
# Copies the last two elements from the Object array to the integer array.
[System.Array]::Copy( $myObjArray, $myObjArray.GetUpperBound(0) - 1, $myIntArray, $myIntArray.GetUpperBound(0) - 1, 2 )
# Prints the values of the modified arrays.
"`nAfter copying the last two elements of the Object array to the integer array,"
"integer array:"
PrintValues ( $myIntArray )
"Object array: "
PrintValues ( $myObjArray )
this script produces the following output:
PS C:\foo> .\display-array1.ps1
Initially,
Integer array:
1
2
3
4
5
Object array:
26
27
28
29
30
After copying the first two elements of the integer array to the Object array,
Integer array:
1
2
3
4
5
Object array:
1
2
28
29
30
After copying the last two elements of the Object array to the integer array,
integer array:
1
2
3
29
30
Object array:
1
2
28
29
30