header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Noob: Help needed for get-childitem without "documents and settings"
Last Post 16 Feb 2011 11:00 AM by BustedFlush. 8 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Resolved
SimonUser is Offline
New Member
New Member
Posts:4
Avatar

--
08 Sep 2010 10:22 PM
    Hi All,
    I have been trying to write a powershell script for nearly a week now that lists all the folders recursively from c:\ and excludes "C:\Documents and Settings" and all of it's sub folders.

    My colleagues and I have been trying many different approches to this like this:

    foreach ($file in $(Get-ChildItem c:\ -Recurse | where { $_.PSIsContainer -and $_.Name -ne 'Documents and Settings' } | gci))
    {
    $file.fullname
    }


    but each time we run it, it displays Documents and Settings each time anyway, even though we have excluded it.
    Obviously, -exclude would have been ideal if it worked in PS v2, but the help file accurately indicates that it does not work properly in this cmdlet.

    The final result is that we would use this script as the basis to set permissions on all folders that it returned, and therefore would not impact any folders in C:\Documents and Settings.
    Any help would be appreciated.
    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    09 Sep 2010 01:35 PM
    Try this:

    Get-ChildItem c:\ -Recurse | where { $_.PSIsContainer -AND $_.FullName -notmatch '^C:\\Documents and Settings'} | ft fullname

    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    SimonUser is Offline
    New Member
    New Member
    Posts:4
    Avatar

    --
    09 Sep 2010 06:23 PM
    Thanks very much Shay, that looks really good.  So much easier than what I was thinking I had to do.  I love how simple PowerShell can be... Just wish I knew how to do it.

    teresap989User is Offline
    New Member
    New Member
    Posts:1
    Avatar

    --
    10 Sep 2010 06:52 PM
    Thanks you for the post, sjc073>hi



    __________________
    [url=http://moviesonlineworld.com]watch free movies online[/url]
    BustedFlushUser is Offline
    New Member
    New Member
    Posts:97
    Avatar

    --
    16 Feb 2011 09:03 AM
    Sorry to drag this thread back from the depths, but there's one thing I'm still confused about....

    I noticed Shay suggested '^C:\\Documents and Settings' as the match criteria, and I didn't follow the logic.

    I tried what I would have used: -notmatch "C:\Documents and Settings".
    That did not work.

    But adding another \ to the C:\ does work (ie. "C:\\Documents and Settings") DOES work.

    What's going on here? Does single quotes matter? Is the ^ some sort of regular expression indicator? Why dual slashes?

    Any help appreciated.
    PoSherLifeUser is Offline
    Basic Member
    Basic Member
    Posts:364
    Avatar

    --
    16 Feb 2011 09:35 AM
    in RegEx \ is an escape character. So, it must be preceeded by another \ to search for the actual slash.

    ^ indicates the search must start at the beginning of the string.

    Great RegEx/PoSh reference:
    http://powershell.com/cs/blogs/eboo...sions.aspx
    When at first you don't succeed Step-Into

    http://theposherlife.blogspot.com
    http://www.jandctravels.com

    BustedFlushUser is Offline
    New Member
    New Member
    Posts:97
    Avatar

    --
    16 Feb 2011 10:22 AM
    OK, that makes sense - at least as much sense as Regex usually do, but is there an advantage of using a Regex at all in this case, as opposed to the literal value?
    PoSherLifeUser is Offline
    Basic Member
    Basic Member
    Posts:364
    Avatar

    --
    16 Feb 2011 10:53 AM
    anytime you use the -match/-replace operators you are using regex by default. It's just the way PoSh was written. The advantage is that regex is a standard and very powerful.
    When at first you don't succeed Step-Into

    http://theposherlife.blogspot.com
    http://www.jandctravels.com

    BustedFlushUser is Offline
    New Member
    New Member
    Posts:97
    Avatar

    --
    16 Feb 2011 11:00 AM
    Ahh, OK - cool, didn'trealize that. Thanks!
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer