Combining csv values into a string

Tagged: 

This topic contains 8 replies, has 3 voices, and was last updated by Profile photo of Tony Antony Tony Antony 1 month ago.

  • Author
    Posts
  • #56884
    Profile photo of Tony Antony
    Tony Antony
    Participant

    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,

  • #56887
    Profile photo of Tony Antony
    Tony Antony
    Participant

    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

    • #56890
      Profile photo of Olaf Soyk
      Olaf Soyk
      Participant
      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

  • #56891
    Profile photo of Tony Antony
    Tony Antony
    Participant

    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
            }      
       }
  • #56899
    Profile photo of Tony Antony
    Tony Antony
    Participant

    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()
  • #56900
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    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"
    }
    
  • #56902
    Profile photo of Rob Simmers
    Rob Simmers
    Participant
    $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
    }
    
  • #56909
    Profile photo of Tony Antony
    Tony Antony
    Participant

    Thank you,

    I'll try that

  • #56915
    Profile photo of Tony Antony
    Tony Antony
    Participant

    Thank you, it's working now.

You must be logged in to reply to this topic.