Hello,
I need to construct a relative path from the directory at the top of the tree...
For example, if the file in question lives at absolute path C:\Users\MyUsername\Desktop\test.txt, and I decide that the root directory is C:\Users\MyUsername, the relative path would be Desktop\test.txt, which is what should be stored in the CSV.
I am going to check for several folders and its contents
In one variable can I use such as this: $variabel1 = "<pathtofolder1>","<pathtofolder2>","<pathtofolder3>"
Could you tell how I can do it whithin my current script please?
Here is my current powershell script:
$root = "C:\Users\"
$hasher = [System.Security.Cryptography.SHA256]::Create()
$AllFiles = @()
foreach ($file in get-childitem $root -recurse | Select-Object FullName, Directory, Name, PSIsContainer, Length, CreationTime, LastAccessTime, LastWriteTime, Path)
{
$acl = get-acl $file.fullname | select-object path,owner,accesstostring,group
$obj = new-object psObject
$obj | Add-Member -membertype noteproperty -name FilePathandName -Value $file.FullName
$obj | Add-Member -MemberType noteproperty -Name Path -Value $file.path
$obj | Add-Member -membertype noteproperty -name Name -Value $file.Name
$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
$obj | Add-Member -MemberType noteproperty -Name Owner -Value $acl.owner
$obj | Add-Member -MemberType noteproperty -Name Group -Value $acl.group
$AllFiles += $obj
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")) }
$obj | Add-Member -MemberType noteproperty -Name MD5Hash -Value $builder.ToString()
}
}
$AllFiles += $obj
$AllFiles |Export-Csv $report –NoTypeInformation