1
votes

I have a csv ($HomeDir) file like this:

Users,Comments,HomeDir
user1,account1,c:\folder1
user2,account2,c:\folder2
user3,account3,c:\folder3

I get succesfully LastAccessTime for each subfolder in 'HomeDir' column with this code:


    $csv = Import-Csv $HomeDir
    foreach ($folder in $csv.HomeDir)  {
                                        Get-ChildItem $folder -ErrorAction SilentlyContinue |
                                            ? {$_.PSIsContainer -eq "True"} | 
                                             Select-Object FullName,  @{Name='LastAccessTime'; Expression={$_.LastAccessTime.ToString('yyyyMMdd')}} |
                                             Sort-Object -Descending -Property LastAccessTime |
                                             Export-Csv $newcsv -NoTypeInformation -Append
                                              }

The result of $newcsv is:

"FullName","LastAccessTime"
"c:\folder1\Sub1","20201223"      
"c:\folder1\Sub1a","20201223"      
"c:\folder1\Sub1b","20201223"      
"c:\folder2\Sub2","20201218"
"c:\folder2\Sub2a","20201218"
"c:\folder3\Sub3","20201212"
"c:\folder3\Sub3a","20201215"
"c:\folder3\Sub3b","20181215"
"c:\folder3\Sub3c","20201011"

The questions is: is there a way to assign the related User based on corresponding 'Users' column? It would also be enough for me to get an output like this

"Users","FullName","LastAccessTime"
"user1","c:\felder1\Sub1","20201223"
"user1","c:\folder1\Sub1a","20201223"
"user1","c:\folder1\Sub1b","20201223"
"user2","c:\folder2\Sub2","20201218"
"user2","c:\folder2\Sub2a","20201218"
"user3","c:\folder3\Sub3","20201212"
"user3","c:\folder3\Sub3a","20201215"
"user3","c:\folder3\Sub3b","20181215"
"user3","c:\folder3\Sub3c","20201011"

Thanks in advance

2
Allow me to give you the standard advice to newcomers: If you accept an answer, you will help future readers by showing them what solved your problem. To accept an answer, click the large ✓ symbol below the large number to the left of the answer (you'll get 2 reputation points). If you have at least 15 reputation points, you can also up-vote other helpful answers (optionally also the accepted one). If your problem isn't solved yet, provide feedback, or, if you found the solution yourself, self-answer.mklement0

2 Answers

1
votes

Instead of pulling the HomeDir property out before the loop, just reference it inside the loop and add the Users property to your Fullname and LastAccessTime properties.

$properties = @{Name='User'; Expression={$user}},
              'FullName',
              @{Name='LastAccessTime'; Expression={$_.LastAccessTime.ToString('yyyyMMdd')}}

Import-Csv $HomeDir | ForEach-Object {
    $user = $_.users
    Get-ChildItem $_.HomeDir -ErrorAction SilentlyContinue | 
        Where-Object {$_.PSIsContainer -eq "True"} | 
            Select-Object $properties |
                Sort-Object -Descending -Property LastAccessTime
} | Export-Csv $newcsv -NoTypeInformation

To make it easier to read I put the properties in a variable beforehand. Also, if you are on powershell version 3 or higher you can use -Directory parameter in lieu of $_.PSIsContainer -eq $true. Finally, if you use the Foreach-Object instead of a foreach loop you can pipe to export and not have to use -Append opening and closing the file several times.

$properties = @{Name='User'; Expression={$user}},
              'FullName',
              @{Name='LastAccessTime'; Expression={$_.LastAccessTime.ToString('yyyyMMdd')}}

Import-Csv $HomeDir | ForEach-Object {
    $user = $_.users
    Get-ChildItem $_.HomeDir -Directory -ErrorAction SilentlyContinue |  
        Select-Object $properties | Sort-Object -Descending -Property LastAccessTime
} | Export-Csv $newcsv -NoTypeInformation
0
votes

I propose my version.

$HomeDir="c:\temp\input.csv"
$newcsv="c:\temp\output.csv"

Import-Csv $HomeDir | %{

#save user name
$User=$_.Users

Get-ChildItem $_.HomeDir -ErrorAction SilentlyContinue -Directory | 
            Sort LastAccessTime -Descending |
                select @{N='Users'; E={$User}}, FullName,  @{N='LastAccessTime'; E={$_.LastAccessTime.ToString('yyyyMMdd')}}

} |  Export-Csv $newcsv -NoType