header1   header
header
header : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
IMPORTANT: PowerShellCommunity.org is moving! - Wednesday, August 15, 2012

PowerShellCommunity.org is moving!  This community software, and the hardware that it sits on, are no longer serving the purposes of this community.  As a result, we have decided to move this community to a new home at PowerShell.org.  PowerShell.org is already up and running with the new community software and in its new location, so please post any new questions that you have on the forums over there instead of posting them on this site.  We've already started getting some great questions from members of the community over there so please, come on over and join us!

While we are going through this transition, this site will remain up for the short term.  New posts may no longer be created on these forums, however replies to existing posts are allowed so that users who posted questions don't have to re-post the same question on the new site.

[UPDATE 28/02/2013] New user registration has been disabled and forums have now been switched to read-only, including for existing posts since all threads that were started should now be completed. If you have a question about content on this site or about PowerShell in general, head over to PowerShell.org and ask it there where there are people actively using the site and answering questions.

If you have any questions, please let us know on the PowerShell.org site.

Thank you,

Kirk "Poshoholic" Munro

 
objects
Last Post 29 Apr 2012 06:36 PM by CalumPowell. 1 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
seabass_whiteyUser is Offline
New Member
New Member
Posts:1
Avatar

--
27 Apr 2012 02:06 PM
    I am working on a script that i can't seem to get the right output. the problem goes like this:

    The script should  determine the storage used by all of the files and directories located in the given directory. The
    output of the script must include the name of the directory (full path) and the number of bytes
    used.
    Create an object as the output of script and put that object on the pipeline.

    this is what i have so far for the script:

    $name=get-childitem | ?{$_.psiscontainer -eq $true } | select fullname
    $path=get-childitem | ?{$_.psiscontainer -eq $true } | select fullname
    $size=get-childitem | ?{$_.psiscontainer -eq $true } | select length


    $dir=New-Object object
    Add-Member -MemberType noteProperty -name name  -value $name -InputObject $dir
    Add-Member -MemberType NoteProperty -name Path -value $path -InputObject $dir
    Add-Member -MemberType NoteProperty -name Bytes -value $size -InputObject $dir

    $dir


    the answer comes out like this:
    name                                    Path                                    Bytes
    ----                                    ----                                    -----
    @{Name=cit241}                          @{FullName=C:\Users\bds\Document... @{length=}

    the  answer i want is
    name                                    Path                                    Bytes
    ----                                    ----                                    -----
    cit241                          C:\Users\bds\Document...          715




    Thanks for the help in advance

    CalumPowellUser is Offline
    New Member
    New Member
    Posts:28
    Avatar

    --
    29 Apr 2012 06:36 PM
    I've attacked the problem in a slightly different manner than you have, but if I understood your problem, this should work:

    $dir = Get-ChildItem | ? { $_.PSIsContainer -eq $true }
    $result = @()
    $dir | % {
    $size = "{0:0.00}" -f ($_ | Get-ChildItem -Recurse | Measure-Object -Property length -Sum).sum
    $newobj = New-Object System.Object
    $newobj | Add-Member -MemberType NoteProperty -Name Name -Value $_.name
    $newobj | Add-Member -MemberType NoteProperty -Name Path -Value $_.fullname
    $newobj | Add-Member -MemberType NoteProperty -Name Size -Value $size
    $result += $newobj
    }
    $result

    You could also use  $size = "{0:0.00}" -f (($_ | Get-ChildItem -Recurse | Measure-Object -Property length -Sum).sum / 1MB) or similar if you would rather have the size in MB (could also be GB or KB).
    Check out my PowerShell blog: www.calumpowell.com
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Many thanks to our original sponsors: Quest Software • SAPIEN Technologies • Compellent • Microsoft footer
    footer   footer