Hello,
I have a CSV of employee first and last names such as this:
First Name, Last Name
Person, 1
Person, 2
Person, 3
I know how to read the first and last names from the csv, but how would I combine those into one string so that it will be
Person 1, Person 2, Person 3?
I already tried
(First Name + Last Name)
and
(First Name + Last Name).ToString()
Thank you,
Also,
I have a variable
$Today = Get-Date -Format yyyy-MM-dd
, so that it’s
2016-11-04
I have another variable
$Tomorrow = (Get-Date).AddDays(1)
, how would I put that in yyyy-MM-dd format also? Now it’s showing
Saturday, November 05, 2016 9:16:09 AM
When I try
(Get-Date -Format yyyy-MM-dd).AddDays(1)
it says “Method invocation failed because [System.String] does not contain a method named ‘AddDays’.”
Thank you,
Tony
Olaf
3
Get-Date (Get-Date).AddDays(1) -Format yyyy-MM-dd
For your first question: could you show your code please? It’s easier then to help you
This is my code:
$CSV_Location = '\\hqfs1\users\tantony\PowerShell\HRSecurityForms\Employees.csv'
$Import_CSV = Import-Csv $CSV_Location
$CSV_Length = $Import_CSV.Count
$Today = Get-Date -Format yyyy-MM-dd
$Tomorrow = Get-Date (Get-Date).AddDays(1) -Format yyyy-MM-dd
$Date_Effective = ($Import_CSV."Date Effective").Trim(" 00:00:00")
$Type_Of_Form = $Import_CSV."Type of Form"
$Employee_Name = (($Import_CSV."First Name") + ($Import_CSV."Last Name")).ToString()
for($Employees = 0; $Employees -lt $CSV_Length; $Employees++)
{
if($Type_Of_Form.GetValue($Employees) -eq "Deletion/Termination")
{
# this works
}
if($Type_Of_Form.GetValue($Employees) -eq "Transfer")
{
# this works
}
if($Type_Of_Form.GetValue($Employees) -eq "Title Change")
{
# this works
}
if($Today -eq $Date_Effective[$Employees])
{
# this works
Write-Host "This is due today, " $Type_Of_Form[$Employees] -ForegroundColor Green
}
}
This is what I need help with now, thank you for helping with the $Tomorrow format
$Employee_Name = (($Import_CSV."First Name") + ($Import_CSV."Last Name")).ToString()
Olaf
6
I don’t know were your Employe name belongs but I probably would try it like this
$CSV_Location = '\\hqfs1\users\tantony\PowerShell\HRSecurityForms\Employees.csv'
$Import_CSV = Import-Csv $CSV_Location
Foreach($Employee in $Import_CSV){
switch ($Employee."Type of Form") {
"Deletion/Termination" { "this works"}
"Transfer" { "this works"}
"Title Change" { "this works"}
Default {}
}
if((Get-Date).Date -eq (Get-Date $Employee."Date Effective").Date)
{
# this works
Write-Host "This is due today, " $Type_Of_Form[$Employees] -ForegroundColor Green
}
$Employee_Name = $Employee."First Name" + " " + $Employee."Last Name"
}
$Employees = Import-Csv $CSV_Location |
Select *,
@{Name="FullName";Expression={"{0} {1}" -f $_."First Name", $_."Last Name"}}
You can simplify the loop a bit as well:
foreach ($employee in $employees) {
$employee.FullName
}
Thank you, it’s working now.