[MSH] get-childitem and file attributes...

Discussion in 'Scripting' started by Mark Rochotte, Jan 30, 2006.

  1. Hi all,

    I would like to get a list of just directories from the get-childitem cmdlet.
    I just can't quite figure this one out.

    Any help would be appreciated.

    Mark Rochotte, Jan 30, 2006
    1. Advertisements

  2. 2 way could be :

    ls | where {$_.MshIsContainer -eq $true}


    (gi .).GetDirectories()

    (will also show hidden)

    gr /\/\o\/\/
    /\\/\\o\\/\\/ [MVP], Jan 30, 2006
    1. Advertisements

  3. Addition,

    this will let you search on more attributes as Directory (IsContainer)
    but MshIsContainer is not provider specific and this is :

    ls | where {$_.Attributes -contains "Directory"}

    but it may be more clear if you use to on the FS provider.
    /\\/\\o\\/\\/ [MVP], Jan 30, 2006
  4. Sorry to keep posting, but last example was wrong,
    as the Contains is done on one string, as it gets casted.
    so will only show directories with no other attributes, as it is not an

    MSH>(gi .).Attributes | gm

    TypeName: System.IO.FileAttributes

    Name MemberType Definition
    ---- ---------- ----------
    CompareTo Method System.Int32 CompareTo(Object target)
    Equals Method System.Boolean Equals(Object obj)
    GetHashCode Method System.Int32 GetHashCode()
    GetType Method System.Type GetType()
    GetTypeCode Method System.TypeCode GetTypeCode()
    ToString Method System.String ToString(), System.String
    ToString(String format, IFormatProvider provider), System.String ToString...
    value__ Property System.Int32 value__ {get;set;}

    this way to do it is OK, but not very readable.

    MSH>ls | where {$_.Attributes -band 16}

    gr /\/\o\/\/
    /\\/\\o\\/\\/ [MVP], Jan 30, 2006
  5. I'm hoping that perhaps for V2 get-childitem will support a -Type property
    (like new-item supports) that will get child items of a certain type e.g.:

    gci -Type Directory

    BTW the above can be simplified to:

    ls | where {$_.MshIsContainer}
    Keith Hill [MVP], Jan 30, 2006
  6. the equality statement isn't strictly necessary:

    gci -force | where { $_.mshiscontainer }

    will get directories (even hidden ones)


    James W. Truher [MSFT]
    Monad Program Management
    Microsoft Corporation
    This posting is provided "AS IS" with no warranties, and confers no rights.
    James Truher [MSFT], Jan 30, 2006
  7. Thank you very much. That will make my life much easier.

    MSH>> (gi .).Attributes | gm
    MSH>> ls | where {$_.Attributes -band 16}
    Mark Rochotte, Jan 30, 2006
  8. Thanks for all the replies.

    Now if I could get this level of support in some other ng's! :)

    Mark Rochotte, Jan 30, 2006
  9. if you ask this question in the DOS NG you mean ;-)

    but for the CMD lovers, this shortcut could be handy (just forget to
    type the space ;-))

    MSH>function dir/ad {gci -force | where { $_.mshiscontainer }}

    gr /\/\o\/\/
    /\\/\\o\\/\\/ [MVP], Jan 30, 2006
  10. Well let me throw in my favorite MSH dir functions :)

    function dir { get-childitem $args -force | sort @{e={$_.MshIsContainer};
    asc=$false},@{e={$_.Name}; asc=$true} }
    function dirw { get-childitem $args -force | sort @{e={$_.MshIsContainer};
    asc=$false},@{e={$_.Name}; asc=$true} | format-wide }
    function dirs { get-childitem $args -force | sort Length }
    function dirt { get-childitem $args -force | sort LastWriteTime }
    function dird { get-childitem $args -force | where { $_.MshIsContainer } }
    Keith Hill [MVP], Jan 30, 2006
  11. Lol,
    to make the party compleet,

    let's post the script I just made for my blog after this thread, (still
    got to post it)
    it Make dir work the DOS way with attributes you can add other switches

    so now you can do

    dir /ah
    dir /ar
    dir /ard

    etc. and even one more as the CMD one ;-)

    you should remove the alias DIR first

    del variable:dir

    gr /\/\o\/\/

    # Dir Function replacement with attributefilter
    # /\/\o\/\/ 2006
    # http://mow001.blogspot.com

    function dir {
    if($args.length -ne 0) {
    $Script:NewArgs = @()
    $Script:filters = @()
    foreach ($arg in $args) {
    Switch ($arg) {
    {$_ -match '-a'} {
    if ($_ -match 'r'){$Script:filters += ' | where {
    $_.attributes -match "ReadOnly"}'}
    if ($_ -match 'd'){$Script:filters += ' | where {
    $_.attributes -match "Directory"}'}
    if ($_ -match 'h'){$Script:filters += ' | where {
    $_.attributes -match "Hidden"}'}
    if ($_ -match 's'){$Script:filters += ' | where {
    $_.attributes -match "System"}'}
    if ($_.remove(0,2) -match 'a'){$Script:filters += ' | where {
    $_.attributes -match "Archive"}'}
    if ($_ -match 'n'){$Script:filters += ' | where {
    $_.attributes -match "NotContentIndexed"}'}
    {$_ -eq '-d'} {$Script:filters += ' | where { $_.mshiscontainer}'}
    default {$Script:NewArgs += $_}
    invoke-command("gci $Script:NewArgs $Script:filters")
    } else {
    /\\/\\o\\/\\/ [MVP], Jan 30, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.