PowerShell Core for Linux Administrators Cookbook
上QQ阅读APP看书,第一时间看更新

How to do it…

  1. We will start by gathering the properties of our home directory. The path to this is stored in the automatic variable known as $HOME:
PS> Get-Item $HOME | Get-Member

Note the TypeName of the object—the very first line—as well as the MemberType column in the table.

  1. Next, check when the home directory was last written to:
PS> (Get-Item $HOME).LastWriteTime
  1. Can we find out when the parent of your $HOME was created?
PS> (Get-Item $HOME).Parent | Get-Member
PS> (Get-Item $HOME).Parent.CreationTime

To reiterate, PowerShell is not case-sensitive. However, to improve readability, we have stuck to the PowerShell convention of using camel case.
  1. What are the members of this object?
PS> (Get-Item $HOME).Parent.CreationTime | Get-Member
  1. Pick out the year:
PS> (Get-Item $HOME).Parent.CreationTime.Year

  1. Next, let's use a method from within the returned object and convert it into plain text. We saw that CreationTime is a DateTime object. Let's convert that into a plain string:
PS> (Get-Item $HOME).Parent.CreationTime.ToString()
  1. Find the type of object returned by the previous command. Press the up arrow key to bring back the previous command and pipe its output to Get-Member. Compare the output of Get-Member without .ToString():
PS> (Get-Item $HOME).Parent.CreationTime.ToString() | Get-Member
PS> (Get-Item $HOME).Parent.CreationTime | Get-Member