Thursday, December 7, 2023

 PowerShell script to get the List of Specific file Types from sub directories as an CSV file

 $csvfile="C:\Test\CSHTMlFileName.csv" #CSV File Path


    $Path = "C:\Code\TestRepository"  #Folder


    $header = "FileName"

    $psObject = New-Object psobject


    Add-Member -InputObject $psobject -MemberType noteproperty -Name $header -Value ""

    $psObject | Export-Csv $csvfile -NoTypeInformation

    $row = 1

#if you remove the -Filter *.cshtml in below Get-ChildItem it will return all files     

$items = Get-ChildItem -Path $Path -Filter *.cshtml -Recurse | Where-Object{$_.GetType() -ne $null} | Select-Object FullName,Name

    foreach($item in $items){

     $file = Get-Item $item.FullName 

        if(-not $file.PSIsContainer) {

         Write-Host  $item.FullName

            Write-Host  $item.FullName.PSIsContainer

        $row = @{

             "FilePath" =  $item.Name #Name of File

             #"FilePath" =  $item.FullName --full path of File

            

        }

        $newRow = New-Object PsObject -Property $row

        Export-Csv $csvfile -inputobject $newrow -append -Force        


        }

    }

   If you need both FullPath and Name of the files in Excel 2 columns you can run the script twice and generate the one list of CSV file with FullPath and 2nd list of CSV file with Name and combine them

No comments:

Post a Comment