Return to Level1Techs.com

PowerShell Power Copying

#1

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"

0 Likes

#2

$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

#3

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

#4

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

#6

@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.

0 Likes

#7

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”
}

0 Likes

#8

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