Posted On December 26, 2019

PowerShell: Working With Arrays

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Working With Arrays

Problem: elements of a System.Array couldn’t be counted

# Example output when an object is a Hashtable type
PS C:\Users\tola> $arr.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Hashtable System.Object

Resolution: convert a Hashtable to an Array type

# Perform explicit casting
PS C:\Users\tola> $x=[Array]$arr

# Example output when an object is an Array type
PS C:\Users\tola> $arr.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array

# Validate that the casting has succeeded
PS C:\Users\tola> $x.gettype()

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array

# Since System Array type can contain objects, its elements can be counted
PS C:\Users\tola> $x.count
1

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Check RPC Services on Remote Windows Machines

This current version is to be more compatible with PoSH 6.0 (by removing work-flow) as…

PowerShell: Adding a User to Local Groups

Adding User(s) to Local Groups # addUserToLocalGroup.ps1 # Version 0.02 $computernames=@( 'SERVER0001', 'SERVER0002' ) $accountsToAdd='domain\user1','domain\user2'…

PowerShell: Fix Email Aliases as Preparation for O365 Migration

Check SMTP Addresses of On Premise Mailboxes function checkSmtpAddresses{ $microsoftDomain1="@kimconnect.mail.onmicrosoft.com" $microsoftDomain2="@kimconnect.onmicrosoft.com" $dotLocal=".local" $onpremMailboxes=Get-Mailbox -ResultSize unlimited…