Importing and Exporting Data to various file formats
Start by making some students
class Student {
[string]$firstName=""
[string]$lastName=""
[int]$score=3
[System.DateTime]$birthday
Student ([string]$firstName,[string]$lastName,[int]$score,[int]$day,[int]$month,[int]$year) {
$this.firstName = $firstName
$this.lastName = $lastName
$this.score = $score
$this.birthday = (get-date -Minute 0 -Second 0 -Millisecond 0 -Hour 0 -Year $year -Day $day -Month $month)
}
Student ([string]$firstName,[string]$lastName,[int]$score,[int]$day,[int]$month) {
$this.firstName = $firstName
$this.lastName = $lastName
$this.score = $score
$this.birthday = (get-date -Minute 0 -Second 0 -Millisecond 0 -Hour 0 -Year 2003 -Day $day -Month $month)
}
Student ([string]$firstName,[string]$lastName,[int]$score,[System.DateTime]$birthday) {
$this.firstName = $firstName
$this.lastName = $lastName
$this.score = $score
$this.birthday = $birthday
}
Student ([string]$firstName,[string]$lastName) {
$this.firstName = $firstName
$this.lastName = $lastName
$this.score = (Get-Random -Minimum 1 -Maximum 10)
$this.birthday = (get-date -Minute 0 -Second 0 -Millisecond 0 -Hour 0 -Year 2003 -Day (Get-Random -Minimum 1 -Maximum 28) -Month (Get-Random -Minimum 1 -Maximum 12))
}
[string] GetFullName( ) {
return ("{0} {1}" -f $this.firstName,$this.lastName)
}
[string] GetRapportLine( ) {
return ("{0} {1} - {2}/10" -f $this.firstName,$this.lastName,$this.score)
}
}
$students = @()
$students += new-object Student("Bob","Sinclair")
$students += new-object Student("Alice","Cooper")
$students += new-object Student("Freddie","Mercury")
Saving to csv
Direct export to csv (but export by default to UTF8)
$csvpath = "C:\d\students.csv"
$students | export-csv -path $csvpath -Delimiter "`t" -Encoding Unicode
Convert to csv
$csvpathc = "C:\d\students-convert.csv"
$students | ConvertTo-Csv -Delimiter ";"
$students | ConvertTo-Csv -Delimiter "`t" | Out-File -FilePath $csvpathc
Open excel
explorer $csvpath
Reading from csv
Convert Direct from csv
$importCSVStudents = Import-Csv -Delimiter "`t" -path $csvpath
$importCSVStudents
Convert from csv
$convertCSVStudents = get-content -path $csvpathc | convertfrom-csv -Delimiter "`t"
$students | gm
$convertCSVStudents | gm
Convert to JSON
Convert to Json
$jsonpath = "c:\d\students.json"
$students | convertto-json
$students | convertto-json | set-content -path $jsonpath
Convert back from JSON
Convert from Json
$jsonpath = "c:\d\students.json"
$importedJsonStudents = get-content -path $jsonpath | convertfrom-json
$importedJsonStudents | gm
Convert back to a real student
function Convertto-Student {
process {
new-object Student($_.firstName,$_.lastName,$_.score,$_.birthday)
}
}
$importedJsonStudents | convertto-student
$importedJsonStudents | convertto-student | gm
Convert to XML
Convert to XML
$xmlpath = "c:\d\students.xml"
$students | convertto-xml
$students | ConvertTo-Xml -As Stream
$students | ConvertTo-Xml -As Stream | set-content -path $xmlpath
Convert back from XML
Convert from XML
$xmlpath = "c:\d\students.xml"
[xml]$xmldocument = get-content -path $xmlpath
$xmldocument | gm
Convert to CLI XML
Convert to CLI XML
$xmlclipath = "c:\d\students-cli.xml"
$students | Export-Clixml -path $xmlclipath
Convert back CLI XML
Convert from CLI XML
$xmlclipath = "c:\d\students-cli.xml"
$clistudents = Import-Clixml -Path $xmlclipath
$clistudents | gm
Adding static convert function
Adding a static class for converting back
class Student {
[string]$firstName=""
[string]$lastName=""
[int]$score=3
[System.DateTime]$birthday
Student ([string]$firstName,[string]$lastName,[int]$score,[int]$day,[int]$month,[int]$year) {
$this.firstName = $firstName
$this.lastName = $lastName
$this.score = $score
$this.birthday = (get-date -Minute 0 -Second 0 -Millisecond 0 -Hour 0 -Year $year -Day $day -Month $month)
}
Student ([string]$firstName,[string]$lastName,[int]$score,[int]$day,[int]$month) {
$this.firstName = $firstName
$this.lastName = $lastName
$this.score = $score
$this.birthday = (get-date -Minute 0 -Second 0 -Millisecond 0 -Hour 0 -Year 2003 -Day $day -Month $month)
}
Student ([string]$firstName,[string]$lastName,[int]$score,[System.DateTime]$birthday) {
$this.firstName = $firstName
$this.lastName = $lastName
$this.score = $score
$this.birthday = $birthday
}
Student ([string]$firstName,[string]$lastName) {
$this.firstName = $firstName
$this.lastName = $lastName
$this.score = (Get-Random -Minimum 1 -Maximum 10)
$this.birthday = (get-date -Minute 0 -Second 0 -Millisecond 0 -Hour 0 -Year 2003 -Day (Get-Random -Minimum 1 -Maximum 28) -Month (Get-Random -Minimum 1 -Maximum 12))
}
[string] GetFullName( ) {
return ("{0} {1}" -f $this.firstName,$this.lastName)
}
[string] GetRapportLine( ) {
return ("{0} {1} - {2}/10" -f $this.firstName,$this.lastName,$this.score)
}
static [Student[]] ConvertFromSerialization([PSObject[]]$psostudents) {
$students = @()
foreach($s in $psostudents) {
$students += new-object Student($s.firstName,$s.lastName,$s.score,$s.birthday)
}
return $students
}
}
$students = @()
$students += new-object Student("Bob","Sinclair")
$students += new-object Student("Alice","Cooper")
$students += new-object Student("Freddie","Mercury")
Reading from csv
Convert Direct from csv
$importCSVStudents = [student]::ConvertFromSerialization((Import-Csv -Delimiter "`t" -path $csvpath))
$importCSVStudents | gm
Convert back from JSON Revised
Convert from Json
$jsonpath = "c:\d\students.json"
$importedJsonStudents = [student]::ConvertFromSerialization((get-content -path $jsonpath | convertfrom-json))
$importedJsonStudents | gm
Convert back CLI XML Revised
Convert from CLI XML
$xmlclipath = "c:\d\students-cli.xml"
$clistudents = [student]::ConvertFromSerialization((Import-Clixml -Path $xmlclipath))
$clistudents | gm