get-childitem variable

Tagged: 

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Arjen Nap Arjen Nap 8 months, 1 week ago.

  • Author
    Posts
  • #37331
    Profile photo of Arjen Nap
    Arjen Nap
    Participant

    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
    Profile photo of Don Jones
    Don Jones
    Keymaster

    $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
    Profile photo of Jonathan Warnken
    Jonathan Warnken
    Participant

    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
    Profile photo of Arjen Nap
    Arjen Nap
    Participant

    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!

You must be logged in to reply to this topic.