Extract Documents to Folder based on Managed Metadata Column - SharePoint

  How to Extract documents from Multiple Document libraries in to Folders. Each has a Managed Metadata Column with Sub Terms. I have created three Functions to  Download Document Create Folder Main Method Addition to above, script will log the Document URL and destination Folder Path in .log file Create Folder   Function   Create-Folder {      param  ( $folderPath )      if  (!( Test-Path  -path  $folderPath ))     {          New-Item   $folderPath  -type directory     } } Download Document ( this is stolen from Stackoverflow 😁 ) Function   Download-Document {      param ( $web ,  $folderPath ,  $docItem )       #File Download Snippet Reffered From : https://stackoverflow.com/questions/43350575/how-to-use-powershell-to-download-files-from-sharepoint        $File  =  $web .GetFile ( $docItem .Url )          $Binary  =  $File .OpenBinary ()          $detinationPath  =  $folderPath  +  "\"  +  $File .Name ;          $Stream  =  New-Object  System.IO.FileStream( $detinationPath

How to Search a Text file for a word using PowerShell

Today I had to find a way to search for specific values in set of text files. I had around 200 files with each has 6000+ records as pipe ( | ) separated File.

This is how I managed to find the file(s) which was  the text.

  1. Check each file for the existence of the word or phrase which I'm looking for.
  2. If word is existing, dig deeper in to the file to find to find the record which is having search word in particular column (in my case it was NTID [windows login name of the user]. 
  3. Same ID might available in different fields manage ID but I wanted to check the User Login ID

I used PowerShell Contains() method and iterate the file data as | separated csv file.

#Source File Path
$filePath = "C:\script\CSV";

$timeStamp = Get-Date;
#All files names which contains the word will be writen in to this file
$NameSearchLogFilePath= "C:\script\logs\NameVerification-$($ExecutionID).log";

#All Recoreds which are maching with LoginID field will be written in to this file
$NameSearchLogFilePathData= "C:\script\logs\NameVerification-$($ExecutionID)-Data.log";

Add-Content -Path $NameSearchLogFilePath -value "Execution ID|FileName|Created|";

 function Get-FilesWithName()


    #get all files in folder
    $files = Get-ChildItem $filePath;

    Write-Host "Number of Files : $($files.Count)";

    foreach ($file in $files)
        $dataFile = "$filePath\$($file.Name)"
        $filedata = [IO.File]::ReadAllText($dataFile);

        #Check the file to check search text existing in the file or not
         $fileExist= $filedata.Contains($searchText);
             #search text is existing in the file and now find the record
            $fileCreatedTime = $file.CreationTime;
            Add-Content -Path $NameSearchLogFilePath -value "$($ExecutionID)|$($file.Name)|$($file.CreationTime)";
            $employeeData = Import-Csv $dataFile -Delimiter "|";
            foreach($record in $employeeData)
                #LoginID is the field which I wanted to check the value
                IF($record.LoginID -eq $searchText)
                    Write-Host "Text Matched.. in $($file.Name) ";
                    Add-Content -Path $NameSearchLogFilePathData -value "$($file.Name) | $($fileCreatedTime) | $($record)";



$searchValue ="searchText";

Get-FilesWithName -searchText $searchValue ;

Hope it might help someone.
If you find better options to achieve same results. let me know in the comments 


Pre-Upgrade Check List

Download Our Free SharePoint Pre-Upgrade Check List

* indicates required

Vintage Clan

Popular posts from this blog

System.Data.SqlClient.SqlException (0x80131904): The EXECUTE permission - User Profile Issue

How to Create Host Header to SharePoint Web Application

SharePoint 2013 Access Denied in Site Collections in Web Application