r/PowerShell • u/bseab1024F • Aug 28 '24
Powershell and Types
Once upon a time I was a C/C++ programmer. I am trying to get my feet wet with PS to accomplish some maintenance tasks on my computer that might not justify either a command line or Windows app. The lack of typing in PS is really messing with my head. It seems as though variables can be created as any type and that "functions" in PS can return a wide variety of types.
How am I supposed to know what is in a variable if I cannot trace it back to its declaration and see a type?
Example question...
$filenames = Get-ChildItem -Path "D:\Brian's Stuff\Media\Data\Video\Santa Barbara\0001 - 0100\Santa Barbara 8493" -Filter *.mp4
So apparently this invocation of Get-ChildItem returns a collection. How do I know the collection type (is it an array? A linked list? An abstraction of a binary tree?) And what about the type of object being stored in the collection. How do I determine that without any variable typing? $filenames is a collection, but of what? And it all can change depending on what type of information I am asking Get-ChildItem for?
Maybe I need to find a strongly typed language. I don't get it. BTW I am literally 6 hours old with PS. Just started. Trying to learn only what I need to learn to get the job done. Don't intend to study the language out of intellectual curiousity.
View all comments
Show parent comment
2
u/jortony Aug 29 '24
I'm not near a machine, but I would encourage you to pipe $YourCollectionObjectVariable into Get-Member then you will find it's type/s in the header (over the properties and methods) as previously described.