get-childitem variable

Welcome Forums General PowerShell Q&A get-childitem variable

This topic contains 3 replies, has 3 voices, and was last updated by

2 years, 8 months ago.

  • Author
  • #37331

    Points: 0
    Rank: Member

    I'm trying to use gci on the results of an earlier gci.
    Let me explain myself.
    I have to search and delete certain files in the homefolders of users.

    So I did first $users = gci c:\homefolders -name to get all the names off the userdirs. After this I want to use gci $users\specificdir or something but I cant find out if I have to use foreach command for that. I'm stuck.

    I'm also using this construction to don't overload the server

  • #37332

    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    $users, in that case, is a collection of File and/or Folder objects. You're passing those to the -Path parameter of GCI, which does not accept File/Folder objects.

    You probably meant...

    $users = Get-ChildItem c:\homefolders -Folder | Select -Expand Name

    Or something along those lines, so that $users contains only folder names as String objects. I'm not certain the above is 100% what you're after as I'm not 100% clear on what you're doing, but hopefully this can help you.

  • #37333

    Points: 0
    Rank: Member

    You will need to use the foreach command because the 1st command could contain an unknown number of objects.

    $users = gci c:\homefolders -name
    foreach($user in $users){gci $user.pspath}
  • #37349

    Points: 0
    Rank: Member

    Thanks Jonathan! With your help I could finish my script.

    $users = gci c:\homefolders -name
    $sign = foreach($user in $users){gci $user\Pwrmenu -filter "*outsig*" -name}
    foreach ($del in $sign) {remove-item $del.pspath}

    now I can hunt down all the files with outsig in it and delete it.
    If I do this with the normal explorer it takes me 5 mins maybe and now just 5 seconds :).. love it!

The topic ‘get-childitem variable’ is closed to new replies.