PowerShell Power Copying

Hey everyone

Hoping to get some guidance on a Powershell script I am trying to create.

Thought I would give some background on what I am trying to accomplish.

Currently I have to manually upload files to 8 different locations. The workflow I am using is to open those locations and paste the file into the folder.

What I am hoping to be able to do is load all the files that need copied to the locations into a container folder and then run the script and have it find the container folder copy those contents and then copy them to all 8 locations.

I have figured out how to get it to copy the contents of the container folder to a single location but cannot seem to get it to more than one. I am ok with having the file paths entered into the script but would really like to have it read them from a text file if that is possible.

This is what I have come up with so far. hopefully its not completely crap.

$folder1 = "C:\users\Thedabler\desktop\project\server 1"
$folder2 = "C:\users\Thedabler\desktop\project\server 2"
$folder3 = "C:\users\Thedabler\desktop\project\server 3"
$folder4 = "C:\users\Thedabler\desktop\project\server 4"
$folder5 = "C:\users\Thedabler\desktop\project\server 5"
$folder6 = "C:\users\Thedabler\desktop\project\server 6"
$folder7 = "C:\users\Thedabler\desktop\project\server 7"
$folder8 = "C:\users\Thedabler\desktop\project\server 8"

$Allfolders = "$folder1","$folder2","$folder3","$folder4","$folder5","$folder6","$folder7","$folder8"

Copy-Item "C:\users\Thedabler\desktop\project\To be Moved\*" -Destination "$Allfolders"

$Folder1 = ā€œF:\Video\Testing\Working\FFMpegā€
$Folder2 = ā€œF:\Video\Testing\Working\FFprobeā€

$AllFolders = $Folder1,$Folder2

foreach ($folder in $AllFolders)
{
Copy-Item -path ā€œF:\Video\Testing\output*ā€ -Destination $Folder
}

try the above

3 Likes

Top tip

learn:

import-csv

that way you donā€™t need to keep modifying your script, etc.

Anything imported from CSV, assuming the first line is a field header, you can treat as an object/record with multiple fields.

e.g., something like

Param (
[(Parameter(mandatory=$False)] [string] $filename = ā€œfolders.csvā€
)
$folders = import-csv $filename
foreach ( $folder in $folders ) {
copy-item $folder.source $folder.destination
}

csv file is in the format of
source,destination
sourcepath1, destinationpath1

Then just copy/paste/edit your CSV file in excel, numbers, whatever. or export from another powershell script, any program that can output csv, etc.

Try to break out your ā€œvariableā€ stuff into parameters, csv files, etc.

That way you can test the shit out of your script, code-sign it, know it works, and forget about modifying it.

If you need to keep editing your script, thats a chance to introduce bugs (or otherwise break it) by fucking it up accidentally, and youā€™d need to re-code-sign it every time you edit it (if youā€™re in an environment that uses code-signing, and that will be more popular moving forward). Far better to just not do that and parameterise as much as you can. i.e., keep data (like hardcoded paths, etc.) out of your script.

May sound over the top for a simple script, but better to get into those habits earlyā€¦

my script assumes youā€™re going to list every file source and destination in the CSV file explicitly (i.e., one line per file, per copy operation), but you can use the general idea to say, read a list of files from somewhere and then copy to a bunch of destinations, but i wrote the above because iā€™d consider that way to be more general purpose and not limited to a specific edge caseā€¦ YMMV

3 Likes

Hey thro

Thank you for the responseā€¦ Importing from a file was what I was wanting to do if possible.
I like the idea of just editing the .csv file instead of goofing around in the script. That way I can change folder/file paths for both the source and destinations, because these can and most likely will change in the future.

Thank you Spider099
I think that helps me figure out the foreach command.

1 Like

@thro

So this is what I have so far

Import-Csv C:\users\thedabler\paths.csv

$filepaths = Import-Csv C:\users\thedabler\paths.csv

Foreach($line in $filepaths)
{

Copy-Item $line.source Destination-$line.Destination

}

I have 2 columns in my .csv file the first column is the file path for the source file it is same for all. the second column is the destination and I have 8 unique file paths entered.

when I try to run the script I am getting the below

Copy-Item : Cannot bind argument to parameter ā€˜Pathā€™ because it is null.

Any input is greatly appreciated.

Believe you want to try this:

Copy-Item -Path $line.source -Destination $line.destination

Also I would test your Import-csv and verify that it is able to get input properly. Test it by doing something like:

$CSV = Import-Csv C:\Thing.csv

ForEach ($Variable in $CSV) {
echo ā€œ$Variable.Source is source, $Variable.Destination is destinationā€
}

Hey @2bitmarksman Thank you for the response. I will try adding -Path to the copy Item line.

I was able to verify it was pulling the correct fields in from the .CSV by using
$filepaths.destination[2]

1 Like