#6 - PowerShell - Project

#PowerShell

#6 - PowerShell - Project

Inception

Hello everyone, This article is part of The PowerShell series, The knowledge in this series is built in sequence, Check out The PowerShell series.

In The last articles, we tried to cover the basics stuff that will help to take a step further to start typing down PowerShell scripts.

Today's Article will Submit a project together, In This project will try to cover the commands and techniques we learned before. ✨


Project Overview

In This Project will Manage file transfer based on the CSV file content, The CSV file contains Three columns Name, Source, Destination.

Will Copy the matched criteria files from The Source Value to The Destination Value.


Project Steps

Let's start out creating our script, Follow the instructions below.

Harden the Script Environment

  • Create Three paths for The Source, Destination, and Script Path.

As viewed here, We have The Main Directory which is Manage_file_Transefer_Project, and under this main dir we have three dirs, pathOne holds some data that will copy to pathTwo and Script will holds the Script and CSV files.

  • Insert dummy files at pathOne dir as below

  • Create The CSV file

    • Go Under the Script dir, And create a text file with CSV extension instead of TXT.

    • Insert that below values at the CSV file, Then save the CSV file with name Catalog_File.csv

💡
while working with script, The standard way is to not read from constant values (i.e. type the paths in the script) instead you should type all variabls are not constant in external csv file and read from it
💡
using csv file to avoid script editing while adding new paths, using csv file to read from and loop on each line in the script, Hence if you want to append a new paths to copy the files you will just apped at the csv file.

Start Script typing

  • Create the Script under script dir

    • Open PowerShell ISE with admin privilege, insert the below.

    • Examine the code comments

💡
Ensure that when you open the csv file using notepad prgram it appears like the blow, the cells delemiter is comma ","

# Try if yourself

#region variables

$Main_Path = "D:\Manage_File_Transefer_Project"
$Date_in_MIN = Get-Date -Format "yyyy-MM-dd-hh-mm" # get the date with 
# formate Year-Month-Day-Hour-minute and save it a $Date_in_MIN Variable

#endregion variables



#region Body

# Read CSV file content and ignore the table header using select-object -skip 

$CatalogFile_Content = Get-Content -Path "$Main_Path\Script\Catalog_File.csv" | Select-Object -Skip 0

# Loop on each line and hold the line value in $line variable
foreach ($line in $CatalogFile_Content) {

    # split the line values in variables, split the cell and store it in variables
    # the csv file default splitter is , 
    $Name = $line.Split(",")[0]  # take the first value which is the name and save it in $Name variable
    $Source = $line.Split(",")[1]  # take the secound value which is the Source and save it in $Source variable
    $Destination = $line.Split(",")[2]  # take the Third value which is the Destination and save it in $Destination variable


    # copy the files that ends with .pdf, Search for endswith function
    Copy-Item -Path "$Source/*.pdf" -Destination "$Destination/$Name-$Date_in_MIN"

    # copy the files that ends with .xlsx
    Copy-Item -Path "$Source/*.xlsx" -Destination "$Destination/$Name-$Date_in_MIN"

    # copy the files that start with Text, Search for startswith function
    Copy-Item -Path "$Source/Text*" -Destination "$Destination/$Name-$Date_in_MIN"

    }

This is a very simple Project. However, to learn more search for endswith and startwith funtions, and try to create a loop inside loop.


That's it, Very straightforward, very fast🚀. Hope this article inspired you and will appreciate your feedback. Thank you.