about_Properties - PowerShell (2024)

  • Article

Short description

Describes how to use object properties in PowerShell.

Long description

PowerShell uses structured collections of information called objects torepresent the items in data stores or the state of the computer. Typically, youwork with objects that are part of the Microsoft .NET Framework, but you canalso create custom objects in PowerShell.

The association between an item and its object is very close. When you changean object, you usually change the item that it represents. For example, whenyou get a file in PowerShell, you don't get the actual file. Instead, you get aFileInfo object that represents the file. When you change the FileInfoobject, the file changes too.

Most objects have properties. Properties are the data that are associated withan object. Different types of object have different properties. For example, aFileInfo object, which represents a file, has an IsReadOnly propertythat contains $True if the file has the read-only attribute and $False ifit doesn't. A DirectoryInfo object, which represents a file systemdirectory, has a Parent property that contains the path to the parentdirectory.

Object properties

To get the properties of an object, use the Get-Member cmdlet. For example,to get the properties of a FileInfo object, use the Get-ChildItem cmdletto get the FileInfo object that represents a file. Then, use a pipelineoperator (|) to send the FileInfo object to Get-Member. The followingcommand gets the pwsh.exe file and sends it to Get-Member. The $PSHOMEautomatic variable contains the path of the PowerShell installation directory.

Get-ChildItem $PSHOME\pwsh.exe | Get-Member

The output of the command lists the members of the FileInfo object. Membersinclude both properties and methods. When you work in PowerShell, you haveaccess to all the members of the objects.

To get only the properties of an object and not the methods, use theMemberType parameter of the Get-Member cmdlet with a value of Property,as shown in the following example.

Get-ChildItem $PSHOME\pwsh.exe | Get-Member -MemberType Property
TypeName: System.IO.FileInfoName MemberType Definition---- ---------- ----------Attributes Property System.IO.FileAttributes Attributes {get;set;}CreationTime Property System.DateTime CreationTime {get;set;}CreationTimeUtc Property System.DateTime CreationTimeUtc {get;set;}Directory Property System.IO.DirectoryInfo Directory {get;}DirectoryName Property System.String DirectoryName {get;}Exists Property System.Boolean Exists {get;}Extension Property System.String Extension {get;}FullName Property System.String FullName {get;}IsReadOnly Property System.Boolean IsReadOnly {get;set;}LastAccessTime Property System.DateTime LastAccessTime {get;set;}LastAccessTimeUtc Property System.DateTime LastAccessTimeUtc {get;set;}LastWriteTime Property System.DateTime LastWriteTime {get;set;}LastWriteTimeUtc Property System.DateTime LastWriteTimeUtc {get;set;}Length Property System.Int64 Length {get;}Name Property System.String Name {get;}

After you find the properties, you can use them in your PowerShell commands.

Property values

Although every object of a specific type has the same properties, the values ofthose properties describe the particular object. For example, everyFileInfo object has a CreationTime property, but the value of thatproperty differs for each file.

The most common way to get the values of the properties of an object is to usethe member access operator (.). Type a reference to the object, such as avariable that contains the object, or a command that gets the object. Then,type the operator (.) followed by the property name.

For example, the following command displays the value of the CreationTimeproperty of the pwsh.exe file. The Get-ChildItem command returns aFileInfo object that represents the pwsh.exe file. The command isenclosed in parentheses to make sure that it's executed before any propertiesare accessed.

(Get-ChildItem $PSHOME\pwsh.exe).CreationTime
Tuesday, June 14, 2022 5:17:14 PM

You can also save an object in a variable and then get its properties using themember access (.) method, as shown in the following example:

$a = Get-ChildItem $PSHOME\pwsh.exe$a.CreationTime
Tuesday, June 14, 2022 5:17:14 PM

You can also use the Select-Object and Format-List cmdlets to display theproperty values of an object. Select-Object and Format-List each have aProperty parameter. You can use the Property parameter to specify oneor more properties and their values. Or, you can use the wildcard character(*) to represent all the properties.

For example, the following command displays the values of all the propertiesof the pwsh.exe file.

Get-ChildItem $PSHOME\pwsh.exe | Format-List -Property *
PSPath : Microsoft.PowerShell.Core\FileSystem::C:\Program Files\PowerShell\7-preview\pwsh.exePSParentPath : Microsoft.PowerShell.Core\FileSystem::C:\Program Files\PowerShell\7-previewPSChildName : pwsh.exePSDrive : CPSProvider : Microsoft.PowerShell.Core\FileSystemPSIsContainer : FalseMode : -a---ModeWithoutHardLink : -a---VersionInfo : File: C:\Program Files\PowerShell\7-preview\pwsh.exe InternalName: pwsh.dll OriginalFilename: pwsh.dll FileVersion: 7.3.0.5 FileDescription: pwsh Product: PowerShell ProductVersion: 7.3.0-preview.5 SHA: cfc237ac85cf24fa760923ace568201c8f3256aa Debug: False Patched: False PreRelease: False PrivateBuild: False SpecialBuild: False Language: Language NeutralBaseName : pwshResolvedTarget : C:\Program Files\PowerShell\7-preview\pwsh.exeTarget :LinkType :Length : 285088DirectoryName : C:\Program Files\PowerShell\7-previewDirectory : C:\Program Files\PowerShell\7-previewIsReadOnly : FalseFullName : C:\Program Files\PowerShell\7-preview\pwsh.exeExtension : .exeName : pwsh.exeExists : TrueCreationTime : 6/14/2022 5:17:14 PMCreationTimeUtc : 6/14/2022 10:17:14 PMLastAccessTime : 7/18/2022 11:32:06 AMLastAccessTimeUtc : 7/18/2022 4:32:06 PMLastWriteTime : 6/14/2022 5:17:14 PMLastWriteTimeUtc : 6/14/2022 10:17:14 PMLinkTarget :Attributes : Archive

Static properties

You can use the static properties of .NET classes in PowerShell. Staticproperties are properties of class, unlike standard properties, which areproperties of an object.

To get the static properties of a class, use the Static parameter of theGet-Member cmdlet. For example, the following command gets the staticproperties of the System.DateTime class.

Get-Date | Get-Member -MemberType Property -Static
TypeName: System.DateTimeName MemberType Definition---- ---------- ----------MaxValue Property static datetime MaxValue {get;}MinValue Property static datetime MinValue {get;}Now Property datetime Now {get;}Today Property datetime Today {get;}UtcNow Property datetime UtcNow {get;}

To get the value of a static property, use the following syntax.

[<ClassName>]::<Property>

For example, the following command gets the value of the UtcNow staticproperty of the System.DateTime class.

[System.DateTime]::UtcNow

Member-access enumeration

Starting in PowerShell 3.0, when you use the member-access operator (.) toaccess a property that doesn't exist on a list collection, PowerShellautomatically enumerates the items in the collection and returns the value ofthe property on each item. For more information, seeabout_Member-Access_Enumeration.

Examples

This command returns the value of the DisplayName property of every servicethat Get-Service returns.

(Get-Service).DisplayName
Application ExperienceApplication Layer Gateway ServiceWindows All-User Install AgentApplication IdentityApplication Information...

All collections have a Count property that returns the number of objects inthe collection.

(Get-Service).Count
176

Starting in PowerShell 3.0, you can get the Count or Length property ofsingleton objects that aren't collections.

(Get-Service Audiosrv).Count
1

However, some objects have a Length property. For example, the Lengthof a string is the number of characters in the string. The Count propertyis the number of instances of the object.

PS> $str = 'string'PS> $str.Length6PS> $str.Count1

If a property exists on the individual objects and on the collection, only thecollection's property is returned.

$collection = @( [pscustomobject]@{length = "foo"} [pscustomobject]@{length = "bar"})# PowerShell returns the collection's Length.$collection.length
2

See also

  • about_Objects
  • about_Member-Access_Enumeration
  • about_Methods
  • Format-List
  • Get-Member
  • Select-Object
about_Properties - PowerShell (2024)
Top Articles
3 Top Stocks With High Dividend Yields | The Motley Fool
Paul Merriman Ultimate Buy and Hold Portfolio Review & ETFs (2024)
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Pearson Correlation Coefficient
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Kerri Lueilwitz

Last Updated:

Views: 5747

Rating: 4.7 / 5 (67 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Kerri Lueilwitz

Birthday: 1992-10-31

Address: Suite 878 3699 Chantelle Roads, Colebury, NC 68599

Phone: +6111989609516

Job: Chief Farming Manager

Hobby: Mycology, Stone skipping, Dowsing, Whittling, Taxidermy, Sand art, Roller skating

Introduction: My name is Kerri Lueilwitz, I am a courageous, gentle, quaint, thankful, outstanding, brave, vast person who loves writing and wants to share my knowledge and understanding with you.