Using Select-Object to Explore Objects

 2 years ago
source link: https://thomasrayner.ca/using-select-object-to-explore-objects/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Using Select-Object to Explore Objects

When you’re first getting started with PowerShell, you may not be aware that sometimes when you run a command to get data, the information returned to the screen is not ALL the information that the command actually returned.

Let me clarify with an example. If you run the Get-ChildItem cmdlet, you’ll get a bit of information back about all the files in whichever directory you specified.

PS> Get-ChildItem c:\temp\demo

    Directory: C:\temp\demo

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----         6/5/2017   8:11 AM              0 thing.txt

This is not all the data that got returned, though. There are far more properties than just Mode, LastWriteTime, Length and Name to be examined. What are they? Well, we can pipe this cmdlet into Select-Object -Property * to see them.

PS> Get-ChildItem c:\temp\demo | Select-Object -Property *

PSPath            : Microsoft.PowerShell.Core\FileSystem::C:\temp\demo\thing.txt
PSParentPath      : Microsoft.PowerShell.Core\FileSystem::C:\temp\demo
PSChildName       : thing.txt
PSDrive           : C
PSProvider        : Microsoft.PowerShell.Core\FileSystem
PSIsContainer     : False
Mode              : -a----
VersionInfo       : File:             C:\temp\demo\thing.txt
                    Debug:            False
                    Patched:          False
                    PreRelease:       False
                    PrivateBuild:     False
                    SpecialBuild:     False

BaseName          : thing
Target            : {}
LinkType          :
Name              : thing.txt
Length            : 0
DirectoryName     : C:\temp\demo
Directory         : C:\temp\demo
IsReadOnly        : False
Exists            : True
FullName          : C:\temp\demo\thing.txt
Extension         : .txt
CreationTime      : 6/5/2017 8:11:04 AM
CreationTimeUtc   : 6/5/2017 2:11:04 PM
LastAccessTime    : 6/5/2017 8:11:04 AM
LastAccessTimeUtc : 6/5/2017 2:11:04 PM
LastWriteTime     : 6/5/2017 8:11:04 AM
LastWriteTimeUtc  : 6/5/2017 2:11:04 PM
Attributes        : Archive

Look at all that goodness. You can select specific properties by replacing the star with the names of the properties you want to see.

PS> Get-ChildItem c:\temp\demo | Select-Object -Property Name, Attributes, IsReadOnly

Name      Attributes IsReadOnly
----      ---------- ----------
thing.txt    Archive      False

Happy scripting!

Written on June 21, 2017

About Joyk

Aggregate valuable and interesting links.
Joyk means Joy of geeK