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.
12
u/surfingoldelephant Aug 28 '24 edited Jun 11 '26
Get-ChildItemdoes not return a collection. It emits scalar (single) objects to the pipeline one-by-one. This applies to most cmdlets.If output is captured to a variable (like in your example), PowerShell will internally collect each emitted object. Upon completion, the resultant variable's value and type is dependent on the number of objects emitted.
AutomationNull, which represents nothing in PowerShell (as opposed to$null, which is different).[Object[]](an array), containing each object emitted.In your example,
Get-ChildItemmay emit[IO.FileInfo]and/or[IO.DirectoryInfo]instances or nothing depending on what type of item (if any) is found. You could limit the result to just files by specifying the-Fileparameter.Reflection can be performed with the
GetType()method.$fileNames.GetType().FullNamewill give you the full type name, which can be looked up using the .NET API browser (providing the type is from Microsoft).Get-Memberis another useful tool.Further reading:
10
u/OlivTheFrog Aug 28 '24 edited Aug 28 '24
Hi u/bseab1024F
with PS, Types are implicit but they can also be declarative (which often avoids a lot of trouble)
ex :
But
What's happen ? The Type of the $C variable was self-determined by PS relative to the type of the first variable.
But you could alos define the type by youself
Now for your example :
See the properties of your collection, the definition column gives you the type for each property.
Over time you will know the type of number of properties, but often you will still have to use
Get-Member(akaGM, it's shorter).Moreover, some types have very interesting methods. I'll give you an example: I need to define if this or that year is a leap year. Ok, I'll make a function to do that. But wait, I'm going to work with dates, so data of type
[DateTime]. Let's see, if there's not something interesting.Summary: So powershell is a strongly typed language as you want, but there is flexibility to have auto-typing.
Regards, welcome in the Powershell community and happy scripting.
P.S. : take care with
Get-ChildItemcmdlet; this return only the files/folder on the first level. If you need more use the parameter-Recurse.A last addtion : https://github.com/PoshCode/PowerShellPracticeAndStyle a lot of useful info
14
u/Eimee_Inkari Aug 28 '24
Get-member will be your friend. The type is typically at the top of the output.
It will also output methods and properties of the variable//output that is being referenced.
3
u/Barious_01 Aug 28 '24
This can be done with vs code. Use the debugger. Set a break point to where the variable should be populated . Then you can inspect what is in that variable. A game changer for me. Was attempting to make sure I was getting variables filled in a foreach loop once passing through the loop I was able to confirm that the proper entry was there.
3
u/rswwalker Aug 28 '24
External cmdlets and functions will have their return value types listed in their Get-Help pages. I recommend using VSCode, ISE or Vim/Nvim+LSP for editing to get quick reference to each external cmdlet/function as you are typing them out.
2
u/icepyrox Aug 28 '24
Get-Member is going to be your friend
Also all objects have a
.GetType()method. So you can$filenames.gettype()can show you have an array and$filenames[0].gettype()can show you have either fileInfo or DirectoryInfo objects (and yes, you can mix and match in an array) and if you need properties of, you can either pipeline toGet-Memberas I said, orFormat-listto view the properties with values.Anyways, one of the strengths of powershe is thst it's not strongly typed. You can cast a lot of things to other things if needed.
2
u/MechaCola Aug 28 '24
You can also call .psobject.properties too
2
u/ZenoArrow Aug 28 '24
It's super simple.
Take any variable you're interested in and add ".GetType()" on the end of it.
This will tell you the object type.
For example, imagine you had a variable named $x , you can use $x.GetType() to get the type of that variable.
2
u/[deleted] Aug 28 '24
How am I supposed to know what is in a variable if I cannot trace it back to its declaration and see a type?
$a = whatever
$a.GetType()
2
u/OPconfused Aug 28 '24
PowerShell is one of the few scripting-oriented languages that supports generic typing conveniently. PS might actually be one of the friendlier options for you.
As others have suggested, you need to pipe into
Get-Memberor use the methodGetType()to obtain the output type from standard or 3rd-party functions. That aside, you can still do a great deal of typing with your custom functions and code.For your custom functions and code, add the type before a variable, e.g.'
[string]$var = 'abc'or[datetime]$date = Get-Date. You can do this for your function parameters as well:In the above function, you can only submit values to the
idparameter that can be coerced into an integer.If you need to type outputs, then you will have to use a class. A class will support full typing of inputs and outputs.
2
u/mrbiggbrain Aug 29 '24
The underlying type is an ArrayList which is a non-generic dotnet collection that uses an array to back a List construct.
But it's important to remember the cmdlet is not returning this collection but rather emitting to the pipeline. The pipeline sees multiple unhandled pipeline objects and it created an ArrayList to hold them.
PowerShell is based on .NET which is a managed language. That means the runtime called the CLR understands types at both compile and runtime.
PowerShell leverages this to provide both the rigidness of static types (as types can be enforced) and the flexibility of dynamic types.
You can define and enforce types if you want to or allow yourself to loosely assign and rely on CLR to throw errors when things are the wrong types.
2
u/Sea-Pirate-2094 Aug 31 '24
All objects (everything is an object in PowerShell) have a method named.GetType() which tells the type and the type it was derived from. Other comments detail the Get-Member cmdlet. Also all objects have a hidden property named .PSObject.TypeNames which contains an array of what I guess is called the type chain which lists every type the object was ever derived from, with index 0 being the current type. You can insert any name you wish at that index but it only seems to appear in the output of Get-Member.
1
u/g3n3 Aug 28 '24
You’ll love
install-psresource classexplorer. It is superget-member. It is an amazing tool for POC-ing .net. Superior to spinning up adotnet new consoleorcsi.exe1
u/Thotaz Aug 28 '24
Commands can declare their output type with an attribute. You can view this data like this:
Some commands don't declare it however:
For those commands your only option is to execute the code and see for yourself what it outputs (
$Var.GetType()and$Var | Get-Membercan be useful for this).For your own functions, you can declare the output type like this:
1
u/BinaryCortex Aug 29 '24
$variable.Get-Type()
1
u/tokenathiest Aug 29 '24 edited Aug 29 '24
PowerShell will do lots of funny things for you depending on the syntax you chose. You may not always get what you expect. I came from C++ and C# and I had to "forget" some of my typiness habits when I started working with PowerShell. Nevertheless, there are some useful things you can do to see what's going on under the hood.
Get-Member $objThis cmdlet will tell you all about the object in question, its type and properties.
$obj.GetType()is a .NET function you can call on any non-null object to get the System.Type descriptor for the object.
In your example, in PowerShell, it's not really important what specific type $filenames actually is. It could be:
$null) orSystem.IO.FileInfoclass) orSystem.IO.FileInfoobjects in a generic collection or arrayDirectoryInfoinstance or a collection ofFileInfoandDirectoryInfoinstances. The point is: you cannot know in advance, so...Regardless of what is actually stored in
$filenameswhen you hand$filenamesto theforeachstatement or pipelineGet-ChildItemtoForEach-Objectthe PowerShell runtime will handle each case automatically. The language is designed around you not knowing or caring what$filenamesactually is, object-wise. This is very different from C++ land.1
u/FluxMango Sep 02 '24
$filenames.GetType().FullName should give you the exact type of collection you are dealing with.
$filenames[0].GetType().Fullname should give you the type of the first element.
The only times I worry about types in PS is when I am using .NET libraries in my code, or need to transform my input or output data.
What makes PS really powerful, is that it is an object based CLI, unlike cmd or Bash which are text based. Meaning that even your output has properties and methods and can be transformed or passed for further processing to another command. You can think of it as a C# lite command shell.
-1
u/lrdmelchett Aug 28 '24
ChatGPT is a good instructor for basics.
Certainly! Here’s a reusable prompt that you can use to request a PowerShell script for declaring variables with types and interrogating their data types: