In this question we resolved our issue but there was one point that I have not learned yet.
Below comments in the above post:
My goal is - To call each file data based on indexing from nested array and remove last three lines. So-
$array = New-Object Sytem.Collections.Arraylist; Get-ChildItem C:\...\test | ForEach-Object { $array += ,@(Get-Content $_.FullName) }; $array[0].removerange($array[0].count-2,2)
But it throws error that removerange is not recognised. I checked -$array[0] | gm
and removerange method was indeed not there. Just Remove and Removeat. How to proceed for this? - iamsmith41 Jan 11 at 22:14@iamsmith41
Get-Content
returns aSystem.Array
, not aSystem.Collections.ArrayList
. The former doesn't have aRemoveRange()
method. Also, please don't move the target. If one of the answers resolves the problem described in your current question: please consider accepting that answer. If you have a new or followup question: please post a new question. - Ansgar Wiechers Jan 11 at 23:33Ok. I marked the answer. But just let me know how to get it done( removerange() method ). Thanks in advance. - iamsmith41 2 days ago
$array += ,[Collections.ArrayList]@(Get-Content $_.FullName)
should probably suffice. If you need further help please post a new question. - Ansgar Wiechers 2 days ago
How to know the object type like above that I have to use is Collections.ArrayList
and so on? How to know that this is a System.Array
and not System.Collections.ArrayList
, etc.?
You can determine the type of an object via its GetType()
method:
PS C:\> (Get-Item '.').GetType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True DirectoryInfo System.IO.FileSystemInfo PS C:\> (Get-Item '.').GetType().FullName System.IO.DirectoryInfo
or by using the Get-Member
cmdlet:
PS C:\> Get-Item '.' | Get-Member TypeName: System.IO.DirectoryInfo Name MemberType Definition ---- ---------- ---------- Mode CodeProperty System.String Mode{get=Mode;} Create Method void Create(), void Create(System.Securi... CreateObjRef Method System.Runtime.Remoting.ObjRef CreateObj... CreateSubdirectory Method System.IO.DirectoryInfo CreateSubdirecto... ...
The former provides meta information about an object, like its name, base type, which assembly its from, etc. (pipe the output of GetType()
into Format-List *
to get a full list).
The latter is mainly for obtaining information about the members (properties and methods) of an object (or the static members of a class if you use the parameter -Static
). Note that if you want information about the members of a collection object you must use Get-Member -InputObject $col
instead just $col | Get-Member
, because using the pipeline would unroll the collection and you'd get the members of the collection elements rather than those of the collection object itself.
Once you know a class you'd normally look up further information in the documentation, e.g. by feeding a class or member name into your preferred search engine.