Power shell получить член

In this guide, we will walk you through the syntax of the Get-Member cmdlet, provide examples of its usage, and explore how to analyze object properties and methods using Get-Member.

Key Takeaways:

  • Using Get-Member, you can explore object properties, methods, and other member types to gain insights into their capabilities.
  • It can be used to specify the object using the InputObject parameter or by piping objects to Get-Member.
  • Get-Member returns a list of members sorted alphabetically, with methods listed first and then properties.
  • The syntax of the Get-Member cmdlet includes various parameters to customize the output.

Table of contents

  • What is the Get-Member cmdlet in PowerShell?
  • Examples of Using Get-Member Cmdlet
  • Exploring Object Properties with Get-Member
  • Conclusion

What is the Get-Member cmdlet in PowerShell?

How to use Get-Member in PowerShell

The Get-Member cmdlet retrieves the members (such as properties, methods, and events) of any object you pipe into it. This allows you to explore what members an object has available so you can determine how to interact with that object in your script.

Syntax of Get-Member Cmdlet

ParameterDescription
-InputObjectSpecifies the object to examine. If not used, Get-Member processes objects passed through the pipeline.
-MemberTypeFilters the output to show only members of a specified type (e.g., methods, properties, events).
-NameAllows specification of the name(s) of one or more members to view. Useful for focusing on specific properties or methods.
-StaticRetrieves static members of the object, which are accessible without instantiating the object.
-ViewSpecifies the types of members to get (e.g., Base, Extended, Adapted, All).
-ForceForces Get-Member to list hidden or otherwise inaccessible members, useful for advanced debugging.
 Get-Member -InputObject <object>

For example, let’s say you want to view the members of the Get-Process cmdlet:

:/>  Наилучшая загрузка цп службой windows

This command will display information about the properties and methods of the objects produced by the Get-Process cmdlet, which retrieves the status of processes running on the local or remote computer. Now, you can see what is available to work with these objects.

get-member

To view the properties of an object, combine Get-Member with Select-Object:

Get-Process | Select-Object -First 1 | Get-Member -MemberType Property | Select-Object Name, Definition

Examples of Using Get-Member Cmdlet

Here are some examples of using the Get-Member cmdlet in PowerShell:

Example 1: Retrieving Member Information

View properties and methods of a string object:

"Hello World" | Get-Member

This will display a list of members sorted alphabetically, with methods listed first and then properties. It provides valuable insights into the structure and capabilities of the object, allowing you to understand how to interact with it effectively.

Example 2: Filtering Member Types

Get-Member -InputObject $User -MemberType Property

To view methods of an object, use:

[DateTime] | Get-Member -Static -MemberType Method

Here is another real-world example: Let’s identify what properties a CSV contains:

Import-Csv C:\temp\DL-Group-Members.csv | Get-Member -MemberType NoteProperty

This helps determine what columns are available in that CSV.

This will filter the output and display only the properties of the object, excluding methods and other member types. It allows you to focus on the specific information you need and simplifies the analysis of the object’s structure.

Example 3: Displaying Members in Different Views

The Get-Member cmdlet also allows you to view the members of an object in different ways using the View parameter. By default, Get-Member displays the members in the ‘Extended’ view. However, you can specify other views such as ‘Base’, ‘Adapted’, or ‘All’ to see different sets of members.

Get-Member -InputObject $User -View Base

This command will display only the base members of the object, excluding any additional members introduced by adaptation or extension. It provides a more focused view of the core members of the object to aid in understanding its fundamental structure.

:/>  Сделать загрузочную флешку Windows 7

Example 4: Using Get-Member with Pipelining

This will pipe each object in the collection to Get-Member, and it will display the properties, methods, and other member types for each object. It allows for efficient analysis of multiple objects in a single command, saving time and effort in exploring their structure and capabilities.

Get-ChildItem "C:\Scripts\AppLog.txt" | Get-Member

This command first uses the “Get-ChildItem” cmdlet to retrieve the file object, and then pipes it to “Get-Member” to view its properties and methods. The output will be a list of properties and methods for the file object, such as “Name,” “Length,” “LastWriteTime,” and “Delete.”

Exploring Object Properties with Get-Member

The list of properties includes both regular properties and alias properties, providing a comprehensive view of the object’s data structure.

Example Object Properties

Property NameData Type
FirstNameString
LastNameString
AgeInt32
EmailString

Let’s see how to get the properties of a “Person” object in PowerShell using Get-Member cmdlet:

$Person = [PSCustomObject]@{ FirstName = 'John' LastName = 'Doe' Age = 35 email = "John.Doe@Crescent.com"
}
$Person | Get-Member
powershell get-command cmdlet

Conclusion

In summary, the Get-Member cmdlet in PowerShell is a powerful tool for exploring and analyzing object properties and methods. By using Get-Member, we can retrieve information about the members of objects and gain insights into their structure and capabilities.

It allows us to better understand how to interact with objects and leverage their functionality in PowerShell scripts and commands. Mastering the usage of Get-Member can greatly enhance our PowerShell skills and enable efficient object manipulation.