How to do it...

We shall first look at converting text into an object from plain text input at the terminal. This involves using what is known as a PowerShell Type Accelerator. A PowerShell Type Accelerator is a an alias for .NET classes. Using these, we can call .NET classes and use many of their functionalities within PowerShell.

  1. Let us take plain text as input and convert the text into a date object. To check what sort of object your input is, use the Get-Member cmdlet.
PS> '21 June 2018' | Get-Member
Enclosing any text within single quotes defines the text as a non-expanding literal string. No explicit definition is required in this case, in PowerShell.
  1. The TypeName says, System.String. This confirms that what we entered was plain text. Let us now use a Type Accelerator and convert this text into a DateTime object. The accelerator for this purpose is [DateTime]; place this accelerator before the literal string.
PS> [DateTime]'21 June 2018'

Thursday, 21 June 2018 00:00:00
  1. Next, find the TypeName of the object that was returned.
PS> [DateTime]'21 June 2018' | Get-Member

TypeName: System.DateTime

Voila, the string has been successfully parsed into date and time!

  1. It is also possible to achieve the same result with the cmdlet, Get-Date, when it is called with the text argument.
PS> Get-Date '21 June 2018'

Thursday, 21 June 2018 00:00:00
  1. Similarly, the TypeName would be:
PS> Get-Date '21 June 2018' | Get-Member

TypeName: System.DateTime
  1. Just like we did in the previous recipe, we can now manipulate the object to show information in a more meaningful way. For instance, if you care only about the year, you would write:
PS> (Get-Date '21 June 2018').Year
2018

The other way of converting text into an object is to use cmdlets that perform such tasks. PowerShell packs a few converter cmdlets, one of which is Import-Csv. You may have noticed that PowerShell usually sends out output in a tabular format. This is a simple representation of objects. And Import-Csv converts data in delimited row-and-column structure into objects, where each row is an instance of the object itself, and each column is a property of the object.

  1. To demonstrate this, let us create a CSV file with the following content in it. At the PowerShell prompt, type:
PS> @'
  1. This would take you to the next line; PowerShell is expecting input. Paste the following sample content at the prompt.
WS,CPU,Id,SI,ProcessName
161226752,23.42,1914,1566,io.elementary.a
199598080,77.84,1050,1040,gnome-shell
216113152,0.67,19250,1566,atom
474685440,619.05,1568,1566,Xorg
1387864064,1890.29,15720,1566,firefox
  1. Go to the next line and enter the following at the >> prompt:
'@ | Out-File sample.csv
You could perform the same operation using the touch command and the text editor of your choice. The goal is to get the content into the sample file.
  1. Next, read the contents of the file using PowerShell.
PS> Get-Content ./sample.csv
WS,CPU,Id,SI,ProcessName
161226752,23.42,1914,1566,io.elementary.a
199598080,77.84,1050,1040,gnome-shell
216113152,0.67,19250,1566,atom
474685440,619.05,1568,1566,Xorg
1387864064,1890.29,15720,1566,firefox
  1. That looks like simple text. Let us look at the type name of the object to confirm that this is indeed plain text. Type in:
PS> Get-Content ./sample.csv | Get-Member

TypeName: System.String
  1. That is plain and simple string. Let us now convert the content into a simple object. This is done using Import-Csv.
Import-Csv ./sample.csv

That should give you a list-like output.

  1. To confirm the output is objects, list out its members.

In general, the content is a custom object, as denoted by PSCustomObject. The columns we had in the CSV are of type NoteProperty, as shown by MemberType.

A NoteProperty is a generic property, whose characteristics are similar to string. While most properties are inherited from .NET, NoteProperty is custom-created within PowerShell, as a name-value pair.

  1. If you would rather look at the content as a table, format the content as a table.
PS> Import-Csv ./sample.csv | Format-Table

That brings us to the end of this recipe. We have successfully converted text into an object. However, note that this is just a simple conversion, and that the output of Import-Csv is still string-like. Although, all of the content is now objects, which are easier to handle in PowerShell.

..................Content has been hidden....................

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