Topic: Get-Item
Alias: gi
Note: I had already touched on the Get-Item cmdlet, but, felt there were a few items I had left out, so, I revisited it to complete the task. The original post is http://learningpcs.blogspot.com/2012/09/powershell-v3-in-year-day-5-get-item.html.
As touched on briefly in my post about core commands PowerShell providers are a major part of the technology. To help deal with data in various stores there are a variety of cmdlets you need to become proficient with in order to truly master PowerShell. One of those cmdlets is Get-Item. At first glance, the help does not indicate just how useful this cmdlet is. In fact, it wasnt until I was working with a co-worker that I saw a glimpse of the real usefulness of this cmdlet. As noted in the help, "Get-Item cmdlet gets the item at the specified location." The help goes on to specify that Get-Item only gets the container object, not the content. So, if we have a file (in a file system provider) Get-Item returns a FileInfo object, not, the content of the file.
For example, the following command demonstrates a simple usage of Get-Item with the C:WindowsSystem32cmd.exe file:
The one noteworthy exception I ran across has to do with IIS. There are undoubtedly many other uses (that is, providers) to provide similar examples of how using a wild card will return a collection of properties. In this case, I am looking at an instance of 2012 Enterprises IIS default app pool. After importing the module I ran this command to see what all was on the box:
Alias: gi
Note: I had already touched on the Get-Item cmdlet, but, felt there were a few items I had left out, so, I revisited it to complete the task. The original post is http://learningpcs.blogspot.com/2012/09/powershell-v3-in-year-day-5-get-item.html.
As touched on briefly in my post about core commands PowerShell providers are a major part of the technology. To help deal with data in various stores there are a variety of cmdlets you need to become proficient with in order to truly master PowerShell. One of those cmdlets is Get-Item. At first glance, the help does not indicate just how useful this cmdlet is. In fact, it wasnt until I was working with a co-worker that I saw a glimpse of the real usefulness of this cmdlet. As noted in the help, "Get-Item cmdlet gets the item at the specified location." The help goes on to specify that Get-Item only gets the container object, not the content. So, if we have a file (in a file system provider) Get-Item returns a FileInfo object, not, the content of the file.
For example, the following command demonstrates a simple usage of Get-Item with the C:WindowsSystem32cmd.exe file:
The result of this command is:get-item C:WindowsSystem32cmd.exe
To be more explicit, and, get feedback on the object type, lets tack on a pipelined call to Get-Member. For the sake of brevity I have truncated the output a good bit:Directory: C:WindowsSystem32Mode LastWriteTime LengthName---- ------------- -----------a--- 11/20/2010 9:23 PM 345088 cmd.exe
As indicated in the help, we do not get file content, but, rather, a FileInfo object. Using Get-Item is a way to reference object containers not contents unless you wildcard the cmdlet.TypeName: System.IO.FileInfoName MemberType Definition---- ---------- ----------Mode CodeProperty System.StringMode{get=Mode;}AppendText Method System.IO.StreamWriterAppendText()CopyTo Method System.IO.FileInfoCopyTo(string destFileName),System.IO.FileInfo CopyTo(string destFileName,bool overwrite)Create Method System.IO.FileStreamCreate()CreateObjRef Method System.Runtime.Remoting.ObjRefCreateObjRef(typerequestedType)
The one noteworthy exception I ran across has to do with IIS. There are undoubtedly many other uses (that is, providers) to provide similar examples of how using a wild card will return a collection of properties. In this case, I am looking at an instance of 2012 Enterprises IIS default app pool. After importing the module I ran this command to see what all was on the box:
Next, I zeroed in on a specific app pool (.NET v 2.0) and stashed it in a variable $2.Get-Item IIS:AppPools*Name State Applications---- ----- ------------.NET v2.0 Started.NET v2.0Classic Started.NET v4.5 Started.NET v4.5Classic StartedClassic .NETAppPool StartedDefaultAppPool Started DefaultWeb Site
When I output the objects contents, I saw this:$2 = Get-Item IIS:AppPools.NET v2.0
To get a better picture, I ran this command:$2Name State Applications---- ----- ------------.NET v2.0 Started
$2 | select *name : .NET v2.0queueLength :1000autoStart :Trueenable32BitAppOnWin64 : FalsemanagedRuntimeVersion : v2.0managedRuntimeLoader :webengine4.dllenableConfigurationOverride : TruemanagedPipelineMode :IntegratedCLRConfigFile :passAnonymousToken :TruestartMode :OnDemandstate :StartedapplicationPoolSid