PowerShell v3 in a Year Day 5 Get Item

After hitting two long about_ topics back to back (  and  ) I decided to randomly grab another: Get-Item. Get-Item is actually a useful cmdlet. Unlike Get-Content, which we will cover later, Get-Item, "Gets the item at the specified location." Ummm, okay. Clear as mud. Lets look at some examples to see perhaps what this really means. First, a file system grab (abbreviated for readability):
PS C:Userswsteele> Get-item .

    Directory: C:Users

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         7/26/2012   3:13 PM            wsteele

PS C:Userswsteele> Get-item . | Get-Member

     TypeName: System.IO.DirectoryInfo

Name                      MemberType     Definition                                                                                
----                      ----------     ----------                                                                                
Mode                      CodeProperty   System.String Mode{get=Mode;}                                                             
Create                    Method         void Create(), void Create(System.Security.AccessControl.DirectorySecurity directorySec...
.
.
.                                                     
Root                      Property       System.IO.DirectoryInfo Root {get;}                                                       
BaseName                  ScriptProperty System.Object BaseName {get=$this.Name;}
As indicated by the  Get-Member cmdlet results it returns a System.IO.DirectoryInfo object. Ah, so, thats what it means. Comparing the two objects,  System.IO.Directory and  System.IO.DirectoryInfo helps clarify what  Get-item does. As noted at MSDN, Directory objects,
Exposes static methods for creating, moving, and enumerating through directories and subdirectories. This class cannot be inherited.
DirectoryInfo objects, on the other hand, 
Exposes instance methods for creating, moving, and enumerating through directories and subdirectories. This class cannot be inherited.
As a side note, it does the same thing with  FileInfo and File objects only, the File/FileInfo objects also "aids in the creation of FileStream objects".

In both cases, Get-Item is the focused on accessing the "info" object. Key here is instance, not, static. The consequence of this can be seen in the members returned on each object. Taking the same example above, I look at my directory as an Directory object and a DirectoryInfo object.

Approach 1: DirectoryInfo:


[System.IO.DirectoryInfo]C:userswsteele |gm | ft name, membertype-AutoSize

Name                          MemberType
----                          ----------
Mode                        CodeProperty
Create                            Method
CreateObjRef                      Method
CreateSubdirectory                Method
Delete                            Method
EnumerateDirectories              Method
EnumerateFiles                    Method
EnumerateFileSystemInfos          Method
Equals                            Method
GetAccessControl                  Method
GetDirectories                    Method
GetFiles                          Method
GetFileSystemInfos                Method
GetHashCode                       Method
GetLifetimeService                Method
GetObjectData                     Method
GetType                           Method
InitializeLifetimeService         Method
MoveTo                            Method
Refresh                           Method
SetAccessControl                  Method
ToString                          Method
Attributes                      Property
CreationTime                    Property
CreationTimeUtc                 Property
Exists                          Property
Extension                       Property
FullName                        Property
LastAccessTime                  Property
LastAccessTimeUtc               Property
LastWriteTime                   Property
LastWriteTimeUtc                Property
Name                            Property
Parent                          Property
Root                

Related Posts by Categories

0 comments:

Post a Comment