Skip to content

Instantly share code, notes, and snippets.

@mikecasas
Created February 2, 2016 21:47
Show Gist options
  • Save mikecasas/00461cc6a9852e5c064c to your computer and use it in GitHub Desktop.
Save mikecasas/00461cc6a9852e5c064c to your computer and use it in GitHub Desktop.
CSV manipulation
cls
function DefaultEmptyDate($anydate) {
if ($anydate) {
$a=[datetime]$anydate
} else {
$a= [datetime]"1/1/1900"
}
return $a.ToShortDateString()
}
#Set-Location($env:userprofile + '\Documents')
$path='\\chnfs3\unixnt$\SmartStreams\Payroll\'
Set-Location($path)
$list = Import-CSV $pwd\empcdf.csv |
Select @{Name="employeeId";Expression={$_."EMP ID"}},
@{Name="retiredOn";Expression={$_."RETIRED DATE"}},
@{Name="calendarDays";Expression={$_."CAL DAYS"}}
$lines=@()
$list.foreach{
$item=New-Object PSObject -prop @{
employeeId=[int]::Parse($_."employeeId");
calendarDays=[int]::Parse($_."calendarDays");
retiredOn=[datetime](DefaultEmptyDate($_."retiredOn"));
}
$lines +=$item
}
$lines | export-csv -notype $pwd\empcdfShort.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment