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: Installing or Including an Application On a Computer or Scripting Session

Sample Usage: PS C:\WINDOWS\system32> includeapp -appName notepadplusplus -appExe notepad++notepadplusplus version 7.91.0.0 already exists.True function includeApp($appName,$appExe=$False,$version){…

PowerShell: Grant Domain Admins Access to Directories

Snippet: # Messing around$shareDrives=@("E","F","G","I","J","N",'O','Q','R','S','T','U','V','W','Z','Y');$subdomain=(net config workstation) -match 'Workstation domain\s+\S+$' -replace '.+?(\S+)$','$1';$domainadmins="$subdomain`\Domain Admins"; $shareDrives | %{"Add-NTFSAccess…

How To Prevent Windows From Automatically Rebooting After Updates

# Add New Registry Key $regHive='REGISTRY::HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU' $keyname='NoAutoRebootWithLoggedOnUsers' $value=1 Set-ItemProperty -Path $regHive -Name $keyname -value $value…