** Update i made it work i needed to put the entire foreach inside a variable and it works
Powershell Foreach overwrites the output in excel
I have a script need help fixing it , the excel output is giving me the last output and overwrite the outputs , in powershell it gives the correct and shows all of the output
Import-Module ImportExcel
$FolderPath = "\\clalit\dfs$\HomeFS\Ram Idan Scripts"
$Acl = Get-Acl -Path $FolderPath
$AccessRules = $Acl.Access
foreach ($Rule in $AccessRules) {
try{
$Username = ($Rule.IdentityReference.Tostring().split("\")[1])
$Permissions = ($Rule.FileSystemRights.Tostring().split(",")[0])
$Email = (Get-ADUser $Username -Properties mail).mail
$all = Get-ADUser $Username -Properties DisplayName,SamAccountName,mail
if( $Permissions -match "Modify"){
$all | Select DisplayName,SamAccountName,mail
}
else{}
}catch{$nul}
}
$all | Select DisplayName,SamAccountName,mail | Export-Excel "$FolderPath\tests.xlsx" -FreezeTopRow -AutoSize
$Username = ($Rule.uses uninitialised variable. Fix that, and reformat the code, there's a lot of excess whitespace.