Wednesday, 15 June 2016

Compare two folder's with PowerShell

Below code is the example of how to compare two folder's with PowerShell. This will help us to know what all files were changed. This script will differentiate file on the bases of length but user can change the variable according to usage.   



###PowerShell Start 

#### Variables 
$folderReference = "C:\test"
$folderDifference = "D:\TP"


$FolderReferenceContents = Get-ChildItem $folderReference -Recurse |
    where-object {-not $_.PSIsContainer}
$FolderDifferenceContents = Get-ChildItem $folderDifference -Recurse |
    where-object {-not $_.PSIsContainer}

#get only files that are on laptop not on server
Compare-Object -ReferenceObject $FolderReferenceContents `
-DifferenceObject $FolderDifferenceContents -Property ('Name', 'Length') -PassThru |
    where-object { $_.SideIndicator -eq '=>'} |
        select "FullName"


###PowerShell END

Thursday, 9 June 2016

How to Zip all files in a folder with Power Shell

How to Zip all files in a folder with Power Shell

I hope below script will help us to zip file in a particular format. 


###Powershell Start
if (-not (test-path "C:\Program Files\7-Zip\7z.exe")) {throw "C:\Program Files\7-Zip\7z.exe needed"}
set-alias sz "C:\Program Files\7-Zip\7z.exe" | Out-Null


#### Variables 

$filePath = "f:\t"
$fileEXT = ".txt"
$ziptype = ".7z"

$bak = Get-ChildItem -Recurse -Path $filePath | Where-Object { $_.Extension -eq $fileEXT }


foreach ($file in $bak) {
                    $name = $file.name
                    $directory = $file.DirectoryName
                    $zipfile = $name.Replace($fileEXT,$ziptype)
                    sz a -tzip "$directory\$zipfile" "$directory\$name"      
                }


###Powershell END