Windows PowerShell banner.

Here is my second attempt at copying folders using Microsoft PowerShell scripting. The task I need to accomplish is to copy a set of folders from a server to another location. My first attempt, PowerShell script to copy folders using a CSV listing, does not have enough flexibility for the task.

The complexity is as follows:

  • each learner has a Documents folder on the server;
  • the folder is named according to the learner’s CEMIS (learner) account name;
  • inside each of these folders is a project folder with the naming convention: Surname Name PAT 2020,
  • I need this folder and its contents;
  • collected per Grade.
  |__ AB123456
  |      |__ A folder
  |      |__ Another folder
  |      |__ Brown Anne PAT 2020
  |      |__ Excel work
  |      |__ Homework.xls
  |__ CD654321
  |      |__ PATDamonsC2020
  |      |__ Hello World.pptx
  |__ EF456789
         |__ FunaniEnochPAT

The following CSV file is created using Excel to concatenate the Learner’s CEMIS number with the path of the directory on the server:

Screenshot of CSV file in open in Excel

The following command copied into PowerShell then takes care of the heavy-lifting:

$folderToFind = "PAT"
$csvImportFile = "C:\Folder\list.csv"
$destinationFolder = "C:\Folder\PAT collection"
Import-CSV $csvImportFile | foreach {

	ForEach-Object {
		Get-ChildItem -Path $_.Source -Recurse | Where-Object { $_.PSIsContainer -and $_.Name.Contains($folderToFind)} |
		Copy-Item  -Destination $destinationFolder -Recurse
		"Output: $_"  

I will continue to refactor the script to make it more portable.

By foxbeefly

PHP / MySQL Developer. HTML, CSS and some JavaScript.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.