locked
How to access the values in a variable created by Get-Childitem RRS feed

  • Question

  • I am brand new to PowerShell scripting and am willing to read the book if I can find the right chapter. I'm trying to write a script to list the count and space used of files with various retention periods. If an index contains a data retention tag file then all the files in that directory and sub-directories share the retention defined by the tag file name.

    The following command selects the directories with one year retention and places the directory names in a variable:

    PS C:\PowerShellScripts> $tag1yr = get-childitem 'c:\Test retention' -recurse |
      where {$_.PSIsContainer -like "*False*"} |
      where {$_.name -like "*1 YEAR*"} |
      ft -property directory

    The following command displays the value of the variable:
    PS C:\PowerShellScripts> $tag1y

    Directory 
    ---------                                                                                  
    C:\Test retention\Lower Level\Lower tst 1 yr tag dir
    C:\Test retention\Tst 1 yr by tag file dir

    This ends up with two directory names (listed above) in the $tag1yr variable. Can anyone tell me how my script can use those values to compare with the directory names of files selected by the following command to determine which ones have one year retention?

    PS C:\PowerShellScripts> get-childitem 'c:\Test retention' -recurse |
      where {$_.PSIsContainer -like "*False*"}

    These didn't work:

    PS C:\PowerShellScripts> $tag1yr[1].Directory  # this produced no results

    PS C:\PowerShellScripts> $tag1yr | foreach-object -process {write-host "This is one object:", $_.Directory}
    This is one object:
    This is one object:
    This is one object:
    This is one object:
    This is one object:
    This is one object:

    The following says there are more than two objects in the array:

    PS C:\PowerShellScripts> $tag1yr.count
    6

    Any help you can provide would be appreciated. I can see the two directory names and they are accurate. I just don't know what command would give those values to my script so it could match them against the directory names of each file in the higher level directory being tested to see which ones are within the one year directories.

    Thanks,

    Terry

    Wednesday, December 1, 2010 11:33 PM

Answers

  • You can also use split-path on the filename to get the parent dir:
    
    Get-ChildItem 'c:\Test retention' -Recurse -Filter '*1 Year*' |where {
      !$_.psiscontainer
    }|foreach{
      split-path $_.fullname
    } |select -Unique
    

    http://twitter.com/toenuff
    write-host ((0..56)|%{if (($_+1)%3 -eq 0){[char][int]("116111101110117102102064103109097105108046099111109"[($_-2)..$_] -join "")}}) -separator ""
    • Proposed as answer by Marco Shaw Friday, December 3, 2010 1:43 AM
    • Marked as answer by Dale Qiao Friday, December 3, 2010 3:00 AM
    Thursday, December 2, 2010 9:11 PM
  • This should do the trick:

    Get-ChildItem 'c:\Test retention' -Recurse -Filter '*1 Year*' |where {
      !$_.psiscontainer
    }|foreach{
      $_.directory.fullname
    } |select -Unique
    

    I think your initial example was only off b/c you were not setting $tag1yr to the files.  You only were setting it to the directories.  The directory property doesn't exist on a directory - only a file.  fyi, if you ever need to get the parent directory of a directory you can use split-path to get that info very easily:

    Split-Path (Get-Item 'C:\Documents and Settings').fullname

    dir -recurse |?{$_.psiscontainer} |% {split-path $_.fullname}


    http://twitter.com/toenuff
    write-host ((0..56)|%{if (($_+1)%3 -eq 0){[char][int]("116111101110117102102064103109097105108046099111109"[($_-2)..$_] -join "")}}) -separator ""
    • Marked as answer by Dale Qiao Friday, December 3, 2010 3:00 AM
    • Unmarked as answer by TLIdaho Friday, December 3, 2010 4:09 PM
    • Marked as answer by TLIdaho Friday, December 3, 2010 4:24 PM
    Thursday, December 2, 2010 9:09 PM

All replies

  • From what I can tell you are trying to get all of the files that exist in a directory that has the name *1 Year*

    If that's the case you would do this:

    Get-ChildItem C:\Dropbox -Recurse -Filter *1 YEAR* |where {
      $_.psiscontainer
    } |foreach {
      Get-ChildItem -Recurse $_.fullname |where {
        !$_.psiscontainer
      }
    }
    

    Couple things to note in this one liner (I broke it down:

    • You can use -Filter with get-childitem to look for *1 Year*
    • You shouldn't use -like when looking for directories - Either {$_.psiscontainer}#for dirs or {!$_.psiscontainer} #for not dirs :  If you really want to check for true and false you should do this: where{$_.psiscontainer -eq $true} or where{$_.psiscontainer -eq $false}
    • because I'm searching for directories the directory property doesn't exist.  I'm using the fullname property instead.
    Let us know if you have any questions or if I didn't understand the scope of what you are trying to do.


    http://twitter.com/toenuff
    write-host ((0..56)|%{if (($_+1)%3 -eq 0){[char][int]("116111101110117102102064103109097105108046099111109"[($_-2)..$_] -join "")}}) -separator ""
    Thursday, December 2, 2010 2:58 AM
  • Thank you Tome for your reply. My full script wants to find all the files for a variety of retention periods. The *1 YEAR* files are one category and can be indicated two ways. One indication is when the directory is named *1 YEAR* and the other is when there is a file within the directory named *1 YEAR*. The file within the directory came about when some of our users resisted changing the directory names to accomodate retention indication. The file name *1 YEAR* within a directory sets all the files within that directory and all sub-directories to 1 YEAR retention the same as if the directory name was *1 YEAR*. I have the script working to report on the files that are within directories named *1 YEAR*, *3 YEAR*, etc. I will change that part to work more like your suggestions. I am now trying to add support to recognize the directories where the retention period is indicated by the file within the directory. The selection code in my original question finds the list of directory names that contain *1 YEAR* files. My question now is how my program (script) can use those values as comparison fields as it reads through all the files in the original search directory to identify which have 1 year retention. My thought is to have a table of directory names for each retention category. Long reply, I hope it is clear. Thanks, Terry
    Thursday, December 2, 2010 5:57 PM
  • This should do the trick:

    Get-ChildItem 'c:\Test retention' -Recurse -Filter '*1 Year*' |where {
      !$_.psiscontainer
    }|foreach{
      $_.directory.fullname
    } |select -Unique
    

    I think your initial example was only off b/c you were not setting $tag1yr to the files.  You only were setting it to the directories.  The directory property doesn't exist on a directory - only a file.  fyi, if you ever need to get the parent directory of a directory you can use split-path to get that info very easily:

    Split-Path (Get-Item 'C:\Documents and Settings').fullname

    dir -recurse |?{$_.psiscontainer} |% {split-path $_.fullname}


    http://twitter.com/toenuff
    write-host ((0..56)|%{if (($_+1)%3 -eq 0){[char][int]("116111101110117102102064103109097105108046099111109"[($_-2)..$_] -join "")}}) -separator ""
    • Marked as answer by Dale Qiao Friday, December 3, 2010 3:00 AM
    • Unmarked as answer by TLIdaho Friday, December 3, 2010 4:09 PM
    • Marked as answer by TLIdaho Friday, December 3, 2010 4:24 PM
    Thursday, December 2, 2010 9:09 PM
  • You can also use split-path on the filename to get the parent dir:
    
    Get-ChildItem 'c:\Test retention' -Recurse -Filter '*1 Year*' |where {
      !$_.psiscontainer
    }|foreach{
      split-path $_.fullname
    } |select -Unique
    

    http://twitter.com/toenuff
    write-host ((0..56)|%{if (($_+1)%3 -eq 0){[char][int]("116111101110117102102064103109097105108046099111109"[($_-2)..$_] -join "")}}) -separator ""
    • Proposed as answer by Marco Shaw Friday, December 3, 2010 1:43 AM
    • Marked as answer by Dale Qiao Friday, December 3, 2010 3:00 AM
    Thursday, December 2, 2010 9:11 PM
  • Thanks again Tome. I think that is going to work!
    Thursday, December 2, 2010 9:37 PM