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.
- 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
- 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
- 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!
- 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
- Similarly, the TypeName would be:
PS> Get-Date '21 June 2018' | Get-Member
TypeName: System.DateTime
- 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.
- To demonstrate this, let us create a CSV file with the following content in it. At the PowerShell prompt, type:
PS> @'
- 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
- Go to the next line and enter the following at the >> prompt:
'@ | Out-File sample.csv
- 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
- 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
- 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.
- 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.
- 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.