1

Hello all and best wishes for 2022!

I have the following CSV file:

Name,EmployeeID,File
JohnDoechebox,0009001,ImageBig1.png
JohnDoechebox,0009001,ImageBig2.png
JohnDoechebox,0009001,ImageFlat1.jpg
JohnDoechebox,0009001,ImageFlat2.jpg
JaneJefferson,0009006,IntDoc.docx
JaneJefferson,0009006,IntDoc2.docx
JaneJefferson,0009006,IntImage.jpg
JaneJefferson,0009006,ExtImage.jpg

I want to import the CSV file, this I can do with Import-CSV. Then I want to foreach the imported CSV file so that all rows get parsed.

I have been testing a bit and I came up with the following:

$Name = @()
$EmployeeID = @()

# Importing the CSV file and fill the array

Import-Csv -Path "C:\Temp\TestNew.csv" |`
    ForEach-Object {
        $Name += $_."Name"
        $EmployeeID += $_."EmployeeID"

    Write-Host "Your name is $($_.Name) and your Employee ID is $($_.EmployeeID)"

    }

This works as expected. So now I want to build it so that I can get all the files for that specific user based on the EmployeeID (because names can be duplicate, EmployeeID is always unique) and output the files by Write-Host. Like this:

Write-Host "Your name is $($_.Name) and your Employee ID is $($_.EmployeeID)" You have the following files:

Later I also want to execute an action with each file to copy it somewhere.

Any help would be greatly apreciated! Thanks in advance!

2
  • Cool guys, thanks so much! This seems to do the job well: ` $List = Import-Csv 'C:\Temp\TestNew.csv' | Group-Object EmployeeID foreach ($User in $List) { Write-Host "Your name is $($User.Group.Name[0]) and your Employee ID is $($User.Name) , You have the following files:" $User.Group.File } ` What I don't understand is: Why does the $($User.Name) outputs the EmployeeID? I have made some changes to also create a folder based on the "Name" column and copy the files of the user (by using the EmployeeID) to that specific folder. Commented Jan 5, 2022 at 15:01
  • Sorry my markup in the comment is broken, idk why Commented Jan 5, 2022 at 15:17

2 Answers 2

1

This can be done as follow

$list = import-csv 'C:\Temp\TestNew.csv' | Group-Object EmployeeID
foreach ($user in $list){

Write-Host "Your name is $($user.group.name[0]) and your Employee ID is $($user.Name) , You have the following files:"

$user.Group.file

}

Sign up to request clarification or add additional context in comments.

4 Comments

Cool guys, thanks so much! This seems to do the job well: $List = Import-Csv 'C:\Temp\TestNew.csv' | Group-Object EmployeeID foreach ($User in $List) { Write-Host "Your name is $($User.Group.Name[0]) and your Employee ID is $($User.Name) , You have the following files:" $User.Group.File } What I don't understand is: Why does the $($User.Name) outputs the EmployeeID? I have made some changes to also create a folder based on the "Name" column and copy the files of the user (by using the EmployeeID) to that specific folder.
The proper way to thank someone for helpful answers is to upvote it and/or mark as solution.
in order to know Why does the $($User.Name) outputs the EmployeeID? you need to see the value of the variable $list , as we grouped the output based on the property Name employeeid
@Doug Maurer, thanks for comment
1

To get all files with a specific EmployeeID value, use the Group-Object cmdlet to group the entries from the CSV by that specific column:

Import-Csv -Path "C:\Temp\TestNew.csv" |Group-Object EmployeeID |ForEach-Object {
    # Get the whole list of file names
    $groupOfFileNames = $_.Group |ForEach-Object File
    # Pick the name from the first entry in the grouped list
    $userName = $_.Group[0].Name

    Write-Host "Your name is $($_.Name) and your Employee ID is $($_.EmployeeID)" 
    Write-Host "You have the following files: [$($groupOfFileNames -join ', ')]"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.