Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Powershell script to list filepath, last modified timestamp and ownership for all files in a folder tree?

$
0
0

Hi Scripting Guys,

I'm trying to build a list of files and sub-folders below a top level folder, this list should include the full file path, last modified date and time and the owner using powershell.

I have found a number of scripts that will display the file path and the last modified date or the owner but not both, I'm struggling to combine the output from both scripts into one CSV file.

$path = 'G:\Info.Mgt\vizdot_exec'

$files = Get-ChildItem $path -Recurse

foreach ($file in $files){

$owner = Get-Acl $file.FullName

$file.fullname, $file.lastwritetime, $owner.owner

}

Generates the following output :

29 February 2012 09:54:54
DOMAIN\USER
G:\Info.Mgt\vizdot_exec\contrib
05 December 2011 17:36:31
DOMAIN\USER
G:\Info.Mgt\vizdot_exec\doc
05 December 2011 17:36:32
DOMAIN\USER
G:\Info.Mgt\vizdot_exec\dot.demo
05 December 2011 17:36:35
DOMAIN\USER
G:\Info.Mgt\vizdot_exec\etc
05 December 2011 17:36:35
DOMAIN\USER

I've tried

foreach ($file in $files){
$owner = Get-Acl $file.FullName
write-host -nonewline $file.fullname $file.lastwritetime $owner.owner
Write-Host ""
}

which generates output like:

G:\Info.Mgt\vizdot_exec\bin 29/02/2012 09:54:54 DOMAIN\USER
G:\Info.Mgt\vizdot_exec\contrib 05/12/2011 17:36:31 DOMAIN\USER
G:\Info.Mgt\vizdot_exec\doc 05/12/2011 17:36:32 DOMAIN\USER
G:\Info.Mgt\vizdot_exec\dot.demo 05/12/2011 17:36:35 DOMAIN\USER
G:\Info.Mgt\vizdot_exec\etc 05/12/2011 17:36:35 DOMAIN\USER

I've tried

foreach ($file in $files){
$data | Add-Member NoteProperty -Name "Filepath" -Value $file.fullname
$data | Add-Member NoteProperty -Name "LastWriteTime" -Value $file.lastwritetime
$data | Add-Member NoteProperty -Name "Owner" -Value $Owner.Owner
}

but this generates errors like:

Add-Member : Cannot add a member with the name "Filepath" because a member with that name already exists. If you want to overwrite the member anyway, use the Force parameter to overwrite it.
At line:2 char:19
+ $data | Add-Member <<<<  NoteProperty -Name "Filepath" -Value $file.fullname
    + CategoryInfo          : InvalidOperation: (@{Filepath=G:\I...LLOYDS\ConlonP}:PSObject) [Add-Member], InvalidOperationException
    + FullyQualifiedErrorId : MemberAlreadyExists,Microsoft.PowerShell.Commands.AddMemberCommand

I'm struggling with this particular powershell technique and would appreciate any adviced that you can offer?

Thanks,

Alun.


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>