get-childitem variable


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

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
  • #37331
    Profile photo of Arjen Nap
    Arjen Nap

    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

    Profile photo of Don Jones
    Don Jones

    $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.

    Profile photo of Jonathan Warnken
    Jonathan Warnken

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

    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!

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.