copy-item multiple times

Welcome Forums General PowerShell Q&A copy-item multiple times

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
1 year, 1 month ago.

  • Author
    Posts
  • #82136

    Participant
    Points: 53
    Rank: Member

    I have a script below to copy a file into another folder multiple times. I have a csv file with 3 names, and it's suppose to copy the file 3 times, but it's only copying it one time. How would I make it copy more than one time?

    $csv_location = '\\arg-fs2\users\tantony\PowerShell\Outlook email signature\employees_info.csv'
    $html_files_location = '\\arg-fs2\users\tantony\PowerShell\Outlook email signature\User Outlook Signatures'
    $master_html_file = '\\arg-fs2\users\tantony\PowerShell\Outlook email signature\htmlCodeMaster.txt'
    $read_csv = Import-Csv -Path $csv_location
    
    foreach ($employees in $read_csv) {
        Remove-Item $html_files_location\* -Force        
        $txt_file_names = ($employees.Name) + ".txt"  
        Copy-Item -Path $master_html_file -Destination $html_files_location\$txt_file_names
    }
  • #82139

    Participant
    Points: 0
    Rank: Member

    It actually is copying 3 times, but you are deleting all of the files each time inside of the loop so the end result is only 1 file remains when it's done.

    You should move

    Remove-Item $html_files_location\* -Force

    outside of the loop

  • #82304

    Participant
    Points: 53
    Rank: Member

    Ofcourse, simple mistake.

    It worked thanks.

The topic ‘copy-item multiple times’ is closed to new replies.