How to do it...

In the recipe, Parsing input from text to object, we used Import-Csv to import a comma-separated values file to convert the data contained within to a PowerShell object. Let us recapitulate what we learnt, however this time, now that we know how to work with objects, we will use the imported content in some way.

Before we import content, let us first export some content into a CSV file. This way, we would have some relevant content to manipulate.

  1. Navigate to the location where you created files for this chapter. List the contents of the directory. While the book uses (and recommends) complete cmdlets even at the console (using tab-completion of course), feel free to use aliases if you want.
PS> Get-ChildItem -Path . | Select-Object Name, FullName, CreationTime, LastWriteTime, Extension, Length
  1. Export the contents to a CSV file.
PS> Get-ChildItem -Path . | Select-Object Name, FullName, CreationTime, LastWriteTime, Extension, Length | Export-Csv ./file-list.csv
  1. Open the file in a spreadsheet processor like LibreOffice Calc or even a text editor to view its contents.
PS> Get-Content ./file-list.csv

That was a plain text representation of the object returned by Get-ChildItem.

  1. Import the contents of the CSV file to convert this text into an object.
PS> Import-Csv ./file-list.csv
  1. Find the type of the object returned by this command.
PS> Import-Csv ./file-list.csv | Get-Member

The object type is System.Management.Automation.PSCustomObject.

  1. Check if this is the same as that returned by Get-ChildItem.
PS> Get-ChildItem . | Get-Member
  1. The object returned by Import-Csv is different. Is it possible to treat it just like we do other objects? Get the CreationTime using the member access operator.
PS> (Import-Csv ./file-list.csv).CreationTime
  1. Get just the year.
PS> (Import-Csv ./file-list.csv).CreationTime.Year
  1. Find the type of object returned by the last command.
PS> (Import-Csv ./file-list.csv).CreationTime | Get-Member

TypeName: System.String
  1. Attempt to convert the LastWriteTime into a DateTime object. Pick just the first record, though.
PS> Get-Date (Import-Csv ./file-list.csv | Select-Object CreationTime -First 1).CreationTime
  1. What if we had to retain all the objects within the object returned by Get-ChildItem, including their object types?
PS> Get-ChildItem -Path . | Export-Clixml ./file-list.xml
  1. Now, import the contents of the XML to the session.
PS> Import-Clixml ./file-list.xml
  1. Find out the type name of the object returned by the import command.
PS> Import-Clixml ./file-list.xml | Get-Member
  1. Pick the CreationTime property and find its type.
PS> (Import-Clixml ./file-list.xml).CreationTime | Get-Member

TypeName: System.DateTime
  1. Pick just the year.
PS> (Import-Clixml ./file-list.xml).CreationTime.Year
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.216.174