Powershell script not getting relative path from others folders

Ed7 96 Reputation points
2021-12-03T16:06:54.41+00:00

Hello,

I current have a script here I am trying to get some info into a CSV file and it does not get the relative path from others folders and keeps to pass the same relative path to all folders and subfolders.

Could someon help me on how to fix this please?

This is my current script:

$starttime = (Get-Date)

Variables

$root = "C:\backup\""

Variable for creating the CSV File

$report = "testtttt.csv"

Process for generating the HASH

$hasher = [System.Security.Cryptography.SHA256]::Create()
$AllFiles = @()

"`n"#line space

Write-Host "Generating the Hash from $root"

Getting information from directories

foreach ($file in get-childitem $root -recurse -File | Select-Object FullName, path, Directory, Name, PSIsContainer, Length, CreationTime, LastAccessTime, LastWriteTime)
{

$acl = get-acl $file.fullname | select-object owner,accesstostring,group

$obj = new-object psObject

Getting the Relative Path

$items = Get-ChildItem -Path $root -Recurse -File
foreach($item in $items) {
$relativePath = $item.FullName.Substring($root.Length)
Write-Host "Debug $relativePath" -ForegroundColor Red
#$obj | Add-Member -MemberType noteproperty -Name Path -Value $relativePath #-force
}

Generating HASH File

if(!$file.PsIsContainer)
{

$inputStream = New-Object IO.StreamReader $file.fullname  
$hashBytes = $hasher.ComputeHash($inputStream.BaseStream)  
$inputStream.Close()  

$builder = New-Object System.Text.StringBuilder  
$hashBytes | Foreach-Object { [void] $builder.Append($_.ToString("X2")) }  

Add info into CSV FILE

$obj | Add-Member -membertype noteproperty -name FilePathandName -Value $file.FullName  
$obj | Add-Member -membertype noteproperty -name Name -Value $file.Name  
$obj | Add-Member -MemberType noteproperty -Name RelativePath -Value $relativePath #-force  
$obj | Add-Member -MemberType noteproperty -Name Hash -Value $builder.ToString()  
$obj | Add-Member -membertype noteproperty -name CreationTime -Value $file.CreationTime  
$obj | Add-Member -MemberType noteproperty -Name LastAccessTime -Value $file.LastAccessTime  
$obj | Add-Member -MemberType noteproperty -Name LastWriteTime -Value $file.LastWriteTime  

Variable to send info to CSV

$AllFiles += $obj
}
}

$AllFiles += $obj

Generating CSV FILE

$AllFiles |Export-Csv $report –NoTypeInformation

"n" Write-Host "$report File has been created " "n"
Write-Host "The script took:"
$endTime = Get-Date
New-TimeSpan -Start $startTime -End $endTime

And this is what I am getting in the CSV File

As you can see in green it sets the same relative path to all folders and subfolders/files

154807-image.png

Microsoft 365 and Office | Development | Office JavaScript API
Microsoft 365 and Office | Development | Other
Windows for business | Windows Server | User experience | PowerShell
Windows for business | Windows Server | User experience | Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. MotoX80 36,401 Reputation points
    2021-12-03T16:41:20.28+00:00
    0 comments No comments

  2. Rich Matheisen 47,901 Reputation points
    2021-12-03T20:25:53.87+00:00

    Look no further than this bit of code:

        #Getting the Relative Path
        $items = Get-ChildItem -Path $root -Recurse -File
        foreach ($item in $items) {
            $relativePath = $item.FullName.Substring($root.Length)
            Write-Host "Debug $relativePath" -ForegroundColor Red
            #$obj | Add-Member -MemberType noteproperty -Name Path -Value $relativePath #-force
        }
    

    $obj will get the relative path of the LAST file processed by the foreach loop EVERY TIME.

    If you're going to post code please use the "Code Sample" editor. It's the 5th icon from the left on the Format Bar -- it has the graphic "101 010" icon.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.