Database Reference
In-Depth Information
5.
Output the raw data from the
DataTable
to text iles formatted as a list and a table,
and to XML and CSV formats:
$dt | Format-List | Out-File .\EmployeesList.HR.txt
$dt | Format-Table -auto | Out-File .\EmployeesTable.HR.txt
$dt.WriteXml(".\Employees.xml")
$dt | Export-CSV .\Employees.csv
6. Create custom output iles by piping the
DataTable
to
Where-Object
to ilter it
down,
Sort-Object
to order it, and
ForEach-Object
to iterate and process it,
appending employee data to text iles:
$dt | Where-Object {$_.job_title -like '*Sales*' -and $_.hire_date
-ge [datetime]"02/01/2004"} `
| Sort-Object {$_.manager, $_.employee} `
| ForEach-Object { `
Add-Content (".\Manager-{0}.HR.txt" -f $_.manager) ("{0},
{1}" -f $_.employee, $_.job_title)
}
7. Invoke the current directory to launch Windows Explorer to see the output iles
created and remove the script's directory from the location stack:
Invoke-Item .
Pop-Location
8. Run the script and inspect the iles that were created:
How it works...
The
Get-ScriptDirectory
function handles some subtle differences between PowerShell
hosts to return the directory where the script is running. This will be used later on to create
output iles. Alternatively we could hardcode the path directly or change the directory to the
desired location with
Set-Location
.
Search WWH ::
Custom Search