Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Overwriting Arrays

Closed Thread
 
Thread Tools Display Modes
Old 12-27-2007   #1 (permalink)
DavidLMeissner
Guest


 

Overwriting Arrays

I think I'm missing something simple here. I have the following
script to get the name, size, and owner of files in a directory.

Each time at $Details += $Detail , every element in $Details gets
overwritten with the current $Detail so my output.txt ends up being a
table with the last record repeated.


$files=Get-ChildItem C:/Scripts

$Detail = Select-Object -InputObject "" Name,Size,Owner

$Details = @()

foreach ($file in $files)
{
$owner=Get-acl $file.FullName

$Detail.Name = $file.FullName
$Detail.Size = $file.length
$Detail.Owner = $owner.owner
$Details += $Detail
}

$Details | out-file output.txt
Old 12-27-2007   #2 (permalink)
Jeff
Guest


 

Re: Overwriting Arrays

On Dec 28, 7:48 am, DavidLMeiss...@xxxxxx wrote:
Quote:

> I think I'm missing something simple here. I have the following
> script to get the name, size, and owner of files in a directory.
>
> Each time at $Details += $Detail , every element in $Details gets
> overwritten with the current $Detail so my output.txt ends up being a
> table with the last record repeated.
>
> $files=Get-ChildItem C:/Scripts
>
> $Detail = Select-Object -InputObject "" Name,Size,Owner
>
> $Details = @()
>
> foreach ($file in $files)
> {
> $owner=Get-acl $file.FullName
>
> $Detail.Name = $file.FullName
> $Detail.Size = $file.length
> $Detail.Owner = $owner.owner
> $Details += $Detail
>
> }
>
> $Details | out-file output.txt
You are using the same $Detail object for each file in your loop.
Move the line where you create the $Detail object into the loop, and
you will see what you expect. Below is a another way to accomplish
what you are doing:

Get-ChildItem | Select-Object `
@{Name="Name"; Expression={$_.Fullname}},
@{Name="Size"; Expression={$_.Length}},
@{Name="Owner"; Expression={(Get-Acl $_).Owner}} |
Out-File output.txt

I hope this helps.

Jeff
Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Overwriting existing folder without merging kccube Vista General 0 12-13-2007 07:31 AM
help overwriting free space goingnuts Vista performance & maintenance 0 11-04-2007 11:01 AM
Copying and Overwriting of Files husky86 Vista General 2 10-20-2007 05:14 PM
Overwriting existing files Jeanne Vista General 5 09-26-2007 06:44 PM
How to avoid overwriting XP Colin Barnhorst Vista General 29 09-19-2006 09:45 AM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50