ctrl_alt_dstry
ctrl_alt_dstry

Reputation: 55

Powershell CSV Directory

I am trying to parse through a set of directories to see what files are in them.

More specifically I have a directory that contains every letter of the alphabet and then some other directories. With in the letter directories there is another letter directory and some others. Within that letter directory there are user names. In the user name directory there are directories and files.

|---------------------------------------------
| root
|   \
|    S
|     \
|      S
|       \
|        Smith, John
|                   \
|                    file1, file2, file3, ...
|----------------------------------------------

This matches the structure that I was speaking about above. I am trying to get the names of the files in a CSV so that I can get the following information: User, File1, File2, File3, Path, LastModified, Created.

The ultimate goal for this is to go through every file and compare the file names to active users in AD, and to see what files each user has for auditing purposes. If there is a better way to do this I am all ears.

I have attempted to do this with the following code but continuously get stuck.

Also the M file is labled M1 while the others are letters, which is why I have the random IF/THEN statement in there.

1

function Get-AccessManagementUsersDocuments(){

    $alph = @("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
    $Path = "...LOCATIONPATH...";
    $savePath = "...SAVEPATH..."

    foreach ($alph in $alph){
        if($alph -ne "M"){
            $filePath = Get-ChildItem -Path $Path/$alph/$alph | % { $_.FullName}
            $fileName = Get-ChildItem -Path $Path/$alph/$alph | % { $_.Name}
        } else {
            $filePath = Get-ChildItem -Path $Path/M1/M | % { $_.FullName}
            $fileName = Get-ChildItem -Path $Path/M1/M | % { $_.Name}
        }

        foreach ($filePath in $filePath){
            Get-ChildItem $filePath | %{$_.Name}
        }
    }
}

Upvotes: 2

Views: 38

Answers (1)

Drew
Drew

Reputation: 4020

Why make it difficult with specifying specific files? This will output a sample below.

I would be careful though, due to the comma in the "Directory".

PSChildName     Directory       FullName                            LastWriteTime
-----------     ---------       --------                            -------------
Testfile.ps1    Smith, John     C:\temp\Smith, John\Testfile.ps1    27/03/2019 10:05:47 AM
Testfile1.csv   Smith, John     C:\temp\Smith, John\Testfile1.csv   18/06/2019 12:46:36 PM
Testfile2.csv   Smith, John     C:\temp\Smith, John\Testfile2.csv   18/06/2019 12:38:07 PM
Testfile3.csv   Smith, John     C:\temp\Smith, John\Testfile3.csv   18/06/2019 3:17:00 PM
Testfile4.csv   Smith, John     C:\temp\Smith, John\Testfile4.csv   18/06/2019 12:58:06 PM

 

$Path = "C:\temp"
$savePath = "...SAVEPATH..."

$Files = Get-ChildItem -Path $Path -File -Recurse | Select PSChildName, Directory, FullName, LastWriteTime
Foreach($file in $files) {
    $File.Directory = ($File.Directory -split '\\')[-1]
}
$Files | export-CSV -Path $savePath -NoTypeInformation

Upvotes: 1

Related Questions