| by Arround The Web | No comments

What are the Get Property Commands in PowerShell

In PowerShell, a property is a class member containing data. A property is the characteristic of an object. An object can have multiple properties such as Name, Mode, or Length. Every object has almost similar properties. However, these properties have a different set of values. Such as the LastAccessTime property value of two items can always be different.

Quick Outline:

What are the Get Property Commands in PowerShell

Summary

What are the Get Property Commands in PowerShell?

The Get Property commands refer to the PowerShell commands list that get the properties of the specified file, item, or object. Get Property commands get the properties of the specified item. These commands include Get-Member, Get-Item, Get-ItemProperty, Select-Object, and Get-ChildItem.

1. Get-Member – Gets the Properties of an Item

The Get-Member command gets the properties of an item or object. It gets the specified object’s members, methods, and properties. The object is specified to it using the InputObject parameter or by piping the object to it. However, to get the object’s properties use the -MemberType parameter and specify the Property value.

Syntax

Here is the syntax of the PowerShell’s Get-Member command:

[Command/Directory] | Get-Member -property [Property Value]

Example 1:

This example will get the properties and not the methods of the given object using Get-Member cmdlet:

Get-Process | Get-Member -MemberType Property

According to the above code:

  • First, specify the Get-Process command or any other desired command.
  • Then, pipe it to the Get-Member command.
  • After that, use the -MemberType parameter and specify the Property value to get only the properties of an item:

Example 2:

This demonstration will get the properties of the specified command using the Get-Member cmdlet and Where condition:

Get-Process | Get-Member | where{$_.MemberType -eq "Property"}

According to the above code:

  • First, the Get-Process command is placed and piped to the Get-Member cmdlet.
  • Then, the Get-Member cmdlet is piped to the Where condition.
  • In the Where condition, the MemberType property is selected which will find values that are equal to the Property instance:

2. Select-Object – Selects Objects or Objects Properties

The Select-Object command selects the objects or properties of an object. It can select a specified number of objects, such as the first five items from the index using the -First parameter and the last five using the -Last parameter.

Syntax

This is the syntax of the Select-Object command:

[Command/Object] | Select-Object -Property Property1, Property2, Property3

Example:

This example will select the specified properties of an object using the Select-Object command:

Get-Command | Select-Object -Property Name, Version, Source

According to the above code:

  • First, specify the Get-Command cmdlet and then pipe it to the Select-Object command.
  • After that, use the -Property parameter and specify the properties to select and display:

3. Get-Item – Gets Items from the Given Path

The Get-Item command gets the items from the provided location. It gets the properties for one item at a time. Get-Item commands do not get the content of the specified item, unlike the Get-Content command. But, it lists down the item’s properties such as its name, mode, or length.

Syntax

Here is the syntax of the PowerShell’s Get-Item command:

Get-Item -path [Folder Address]

Example:

This example will get the item from the provided location and will also provide its properties:

Get-Item -path C:\Documents\NewFile.txt

In accordance with the mentioned command:

  • First, specify the Get-Item command.
  • Then, use the -Path parameter and assign it the item’s path:

4. Get-ItemProperty – Gets Properties of the Specified Item

The Get-ItemProperty command gets the properties of the specified item. It gets properties like LastWriteTime, Length, and Mode of the specified item.

Syntax

Here is the syntax of the PowerShell’s Get-ItemProperty command:

Get-ItemProperty -path [Item Address]

Example:

This example gets the properties of a single specified item using the Get-ItemProperty command:

Get-ItemProperty -path C:\Documents\NewFile.txt

According to the above code:

  • First, specify the Get-ItemProperty command.
  • After that, specify the file path using the -path parameter:

5. Get-ChildItem – Gets Child Item Properties from Sub Folders

The Get-ChildItem command gets the items and child items from the specified locations. The subfolders or files are known as child items. It gets the properties of items like Name, Mode, Length, and LastWriteTime.

Syntax

Here is the syntax of the PowerShell’s Get-ChildItem command:

Get-ChildItem -path [Folder Address]

Example:

This example not only gets the child items from the specified location but displays the properties of items:

Get-ChildItem -path C:\Documents\Files

According to the above code:

  • First, mention the Get-ChildItem command.
  • Then, the folder path is specified to the Get-ChildItem command using the -path parameter:

Summary

A property is a piece of information that describes the object. PowerShell Get Property commands are the commands that get the properties of an item, file, folder, or object. These commands include Get-Member, Get-Item, Get-ItemProperty, Select-Object, and Get-ChildItem. Each command either gets the properties of a file or an object.

Share Button

Source: linuxhint.com

Leave a Reply