- Powershell Core 6.2 Cookbook
- Jan Hendrik Peters
- 474字
- 2021-06-24 15:14:31
How to do it...
Perform the following steps:
- Instantiate a new object and store it in a variable:
$folder = Get-Item -Path .
- To find out what the object can offer, we can use the Get-Member cmdlet:
# Either pipe a collection of objects to get member
$folder | Get-Member
# or pass a single object to the parameter InputObject
Get-Member -InputObject $folder
- To filter the output to properties only, we can use the MemberType parameter:
# Filter the output a bit with the MemberType parameter
$folder | Get-Member -MemberType Properties
- Let's examine some members of this class: FullName, Exists, Parent, and *Time:
# Select only a subset of member with the Name parameter
$folder | Get-Member -Name FullName,Parent,*Time, Exists
Notice the definition of each number in the output of Step 4. FullName, Exists; the different Time properties are simple data types such as string, Boolean values, and timestamps. The Parent property, however, is of the System.IO.DirectoryInfo data type:

Try passing the Parent property to Get-Member next – what do you notice when comparing the output with Step 2?
# As long as an object exists on the output, you can deploy Get-Member
$folder.Parent | Get-Member
If you are not sure which data type an object has, there is always the GetType method. This method exists on any .NET object and is inherited from the base class System.Object:
# Our $folder is apparently of the same data type as its parent.
# This should not come as a surprise, since the parent is also a directory.
# You can see the type of any object with GetType()
$folder.GetType().FullName # System.IO.DirectoryInfo
$folder.Parent.GetType().FullName # System.IO.DirectoryInfo
Try the following code and examine the following object properties and methods as well:
# Everything is an object, really.
42 | Get-Member
# External applications will always return string arrays
# Whether it is Linux
mount | Get-Member
$(mount).GetType().FullName # System.Object[] - a list of strings, one for each line
# or Windows
ipconfig | Get-Member
$(ipconfig).GetType().FullName # System.Object[] - a list of strings, one for each line
# Are you missing the exit code of your external application? It is recorded automatically
$LASTEXITCODE
The most basic data types that we use on a day-to-day basis are as follows:
# The most basic classes that we use have so-called type accelerators
[bool] # System.Boolean, the boolean values 0,1,$false,$true
[int16] # Also referred to as short: 16bit integers
[int] # 32bit integers
[int64] # Also referred to as long: 64bit integers
[string] # A series of UTF16 code units or characters
[char] # A single UTF16 code unit or character
[datetime] # A timestamp
[timespan] # A timespan
[array] # A list of objects
[hashtable] # A collection of key-value-pairs
The aforementioned list is, of course, non-exhaustive. You will find yourself using other data types frequently as well.