TEXT

Monday, December 28, 2020

Merge CSV files by using PowerShell

  

Merge CSV files by using PowerShell

In the example below we will merge two CSV files into single CSV file bu using PowerShell. You can use same lines listed below and combine as many as CSV file you like. New CSV file will only contain unique values. Here is goes, as usual change some of the vars listed below to make it fit into your scenario

 

 

#(1)_.Path for CSV Files that needs to be comppared

$File1 = "C:\Users\12024\OneDrive - wiretecpro\Desktop\Compare-CSV\Data1-Computers.csv"

$File2 = "C:\Users\12024\OneDrive - wiretecpro\Desktop\Compare-CSV\Data2-Computers.csv"

 

#(2)_.Import CSV Files

$CSV1 = Import-Csv $File1

$CSV2 = Import-Csv $File2

 

#(3)_.Combine values to Single VAR

$mergedCSV = $csv1 + $csv2

 

#(4)_.Show Combined records

$mergedCSV | Select -Property * -Unique

 

#(5)_.EXPORT Combined FILES

$ExportFile = "C:\Users\12024\OneDrive - wiretecpro\Desktop\Compare-CSV\Combined.CSV"

$mergedCSV | Export-Csv -Path $ExportCSV

 

 

 

Casey, Dedeal

Azure Solutions Architect

AWS Certified Cloud Practitioner

 

https://simplepowershell.blogspot.com

 

https://cloudsec365.blogspot.com

 

https://msazure365.blogspot.com

 

https://twitter.com/Message_Talk

 

 


Sunday, December 27, 2020

Compare two CSV files via PowerShell to find out difference and export results into new CSV file.

Compare two CSV files via PowerShell to find out difference and export results into new CSV file.

If you are looking for simple script can read two CSV file, which each CSV file has multiple columns, then compare them to find out the difference this script is just going to do that. Simply download and adjust few variables to make it work for your scenario.

I have added both CSV file content into script for you to see what script is pulling then variables you can change to make it fit into your needs.

Adjust your CSV file, if your columns are different, you have more columns etc. that is fine, just adjust them accordingly.

“Computername, and UserName “ are column name on the example below, thus script reflect both variables, and also PS custom object, just adjust them.

 Enjoy it.

<#

 

#$File1

 

ComputerName UserName

------------ --------

XLT-001      Allison1

XLT-002      Allison2

XLT-003      Allison3

XLT-004      Allison4

XLT-005      Allison5

XLT-006      Allison6

XLT-007      Allison7

XLT-008      Allison8

XLT-009      Allison9

XLT-010      Allison10

WRK-010      Clint

 

#$File2

ComputerName UserName

------------ --------

XLT-001      Allison1

XLT-002      Allison2

XLT-003      Allison3

XLT-004      Allison4

XLT-005      Allison5

XLT-006      Allison6

XLT-007      Allison7

WRK-001      RAMBO   

XLT-008      Allison8

XLT-009      Allison9

XLT-010      Allison10

 

 

#>

 

 

 

#(1)_.Path for CSV Files that needs to be comppared

$CSV1 = "C:\Users\12024\OneDrive - wiretecpro\Desktop\Compare-CSV\Excell\File1.csv"

$CSV2 = "C:\Users\12024\OneDrive - wiretecpro\Desktop\Compare-CSV\Excell\File2.csv"

 

 

 

#(2)_.Path for export results to CSV

$CSV3 = "C:\Users\12024\OneDrive - wiretecpro\Desktop\Compare-CSV\Excell\DIFFERENCE.csv"

 

 

 

#(3)_.Import both CSV Files you would like to compare

$File1 = Import-Csv $CSV1

$File2 = Import-Csv $CSV2

 

 

#(4)_.Compare both CSV files - column ComputerName,UserName

 

$Results = Compare-Object  $File1 $File2 -Property ComputerName,UserName -IncludeEqual

 

 #(5)_.Set Array

$Array = @()

 

#(6)_.Loop Starts     

Foreach($item in $Results)

{

    If( $item.sideindicator -notcontains "==" )

    {

        $Object = [pscustomobject][ordered] @{

            ComputerName      = $item.ComputerName

            UserName          = $item.UserName

            Compare_Indicator = $item.sideindicator

 

        }

        $Array += $Object

    }

}

 

#(7)_.Count users in both files

($Array | sort-object ComputerName | Select-Object * -Unique).count

 

#(8)_.Display results in console

$Array

 

#(9)_.Export results into CSV File

$Array | export-csv -Path $CSV3 -NoTypeInformation

 

 

Casey, Dedeal

Principal Systems Engineer

https://simplepowershell.blogspot.com/  (blog)

https://smtp25.blogspot.com/ (blog)

https://telnet25.wordpress.com/ (blog)

https://twitter.com/Message_Talk (Twitter)

 


Saturday, December 12, 2020

BACKUP YOUR FILES WITH SIMPLE POWERSHELL FUNCTION

 

BACKUP YOUR FILES WITH SIMPLE POWERSHELL FUNCTION

Following PowerShell will help you to create backup of any desired folder and its content. When running this simple PowerShell function, you will need to provide source and destination variables as shown below.

 

  # Add VARS

 $BackSource = "C:\Users\12024\OneDrive - wiretecpro\Documents\S_Root\"

 $DestSource = "C:\Users\12024\OneDrive - wiretecpro\Documents\Backup\"

 

 

  function Function-Backup{

   

    [CmdletBinding()]

    param(

    [Parameter(Position=0,mandatory=$true)]

    [string] $BackSource,

    [string] $DestSource

    )

 

Try{

 

# Add VARS , start with backup Folder Name

$folderName = 'MyBackup\'

 

# Consantrate Vars

$now = (get-Date -format 'dd-MMM-yyyy-HH-mm-ss-tt')

$BackupFolder  = $folderName+$now

$DestSourceFol =  $DestSource+$BackupFolder

 

# Check DESTINATION Folder, create one if does not exist

if(!(Test-Path  $DestSourceFol)){

 

New-Item $DestSourceFol -ItemType Directory | Out-Null

 

}

 

# Check SOURCE Folder, Stop script if source folder path is not valid

if(!(Test-Path $BackSource)){

Write-Warning 'CANNOT Locate Source'

Write-Warning 'Script will stop'

Start-Sleep -Seconds 3

break;

 

}

 

# Collect all files

$files = (Get-ChildItem $BackSource -recurse)

Write-host 'Backup in progress' -ForegroundColor Green

 

# Start Item by Item Copy

foreach ($file in $files)

{

    if ($file.LastWriteTime -lt ($(Get-Date).AddDays(-1)))

    {  

     Copy-Item $file.FullName -Destination $DestSourceFol -Recurse

 

    }

}

 

# Complete Backup

Write-Host "Completed backup"

 

}Catch{

  $errormessage = $($PSItem.ToString())

  Write-Warning 'Error has occoured'

  Write-host 'Problem FOUND:' $errormessage -ForegroundColor Red -BackgroundColor White

      }

  }

 

 

 

 

  # Add VARS

 $BackSource = "C:\Users\12024\OneDrive - wiretecpro\Documents\S_Root\"

 $DestSource = "C:\Users\12024\OneDrive - wiretecpro\Documents\Backup\"

 

 # Run the functio

 Function-Backup -BackSource $BackSource -DestSource $DestSource 

 

 

Casey, Dedeal

Principal Systems Engineer

https://simplepowershell.blogspot.com/  (blog)

https://smtp25.blogspot.com/ (blog)

https://telnet25.wordpress.com/ (blog)

https://twitter.com/Message_Talk (Twitter)

Simple Function Do Something

   Use template below to create simple PS function    # Simple Function Do Something # Change $ReportName   function Fu...