How does one find the "user/owner" of a file/directory and "Date Modified" via PowerShell?

110 Views Asked by At

Trying to create a Get-Child command that lists Mode, File (path\name), CreationTime, LastWriteTime, LastAccessTime, Length, and the User/Owner of the file/directories listed. I need to do this both for a main directory, but also recursively if there are subfolders (I know about "-recurse", but with the 190 character display limit, The path usually maxes out things.

I then copy/paste said results into Excel where I have some formulas doing some text extraction, and the final product is an Excel sheet with all the strings in their own little cells, and dates formatted properly (and as dates) in their respective cells.

With the 190 character length limitation per row, I'm finding I have to run two commands instead of one, then munge the two together to get all the information I need.

I have so far gotten this far:

Get-ChildItem | Select-Object Mode, PSPath, CreationTime, LastWriteTime, LastAccessTime | Format-Table -AutoSize

I am trying to find the file properties associated with what Windows calls "Owner", so that I can ask PowerShell to create a nifty inventory of files in a directory. I've tried "Owner" and "User" as attributes, but no luck. Windows has Date Modified and Date Last Saved; for the large part they seem to be identical, but I have run into instances where they are not, so looking for those two attributes separately as well.

Also, if anyone knows a way to change PowerShell's screen display limits so as to display more than 190 characters per row, that would also be appreciated.

1

There are 1 best solutions below

0
Theo On

As commented, you don't want to use any Format-* cmdlets if you want to use the retrieved data any further.
Since you want to open it in Excel, the best way is to use Export-Csv with the -UseCulture switch appended, so you can simply double-click the file to have it opened in Excel.

To get the owner of a file or directory, you need an extra calculated property called 'Owner' and the value you get by using Get-Acl.

$sourcePath = 'D:\Test'              # enter your path here
$outputPath = 'D:\Test\output.csv'   # and the path for the output file here

Get-ChildItem -Path $sourcePath -Recurse | 
Select-Object Mode, FullName, CreationTime, LastWriteTime, LastAccessTime,
              @{Name = 'Owner'; Expression = {(Get-Acl -Path $_.FullName).Owner}} |
Export-Csv -Path $outputPath -UseCulture -NoTypeInformation

P.S. The LastWriteTime is the Date Modified