1

Working on a script to get all files on a server and throwing their properties into a CSV. Below works fine, however, I'd like to add some file and folder exclusions.

For folder, I'd like to skip the cookies folder USERS\name\Personal\sysdata\cookies

For files, I'd like to exclude .lnk and .url files.

Thank you.

$arr = @()
gci "D:\COMPANY\USERS\a, user" -recurse 
| ? {$_.PSIsContainer -eq $False} 
| % {
    $obj = New-Object PSObject
    $obj | Add-Member NoteProperty Directory $_.DirectoryName
    $obj | Add-Member NoteProperty Name $_.Name
    $obj | Add-Member NoteProperty MB ("{0:N3}" -f ($_.Length/1MB))
    $obj | Add-Member NoteProperty created $_.creationtime
    $obj | Add-Member NoteProperty LastAccessed $_.LastAccessTime
    $obj | Add-Member NoteProperty LastMofified $_.LastWriteTime
    $obj | Add-Member NoteProperty Extension $_.Extension
    $obj | Add-Member NoteProperty Owner ((Get-ACL $_.FullName).Owner)

    $arr += $obj
    }
$arr | Export-CSV -notypeinformation "C:\filesTest.csv"

1 Answer 1

2

It uses regex so it's not super speedy, but try appending the filter in line 3:

| ? {$_.PSIsContainer -eq $False -and $_.Extension -notin ".lnk",".url" -and $_.Directory -notmatch ".*USERS\\.*\\Personal\\sysdata\\cookies"}
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you. getting this error You must provide a value expression on the right-hand side of the '-' operator. At C:\Users\jg\Desktop\CBFleetFiles.ps1:2 char:110 + gci "D:\USER_FILES\Company\USERS\A, test" -recurse | ? {$_.PSIsContainer -eq $False -and $_.Extensi on - <<<< notin ".lnk",".url" -and $_.Directory -notmatch ".*USERS\\.*\\Personal\\sysdata\\cookies"} | % { + CategoryInfo : ParserError: (:) [], ParseException + FullyQualifiedErrorId : ExpectedValueExpression
nm, its -ne instead of -notin. That worked for skipping the folder, but the URL and LNK files are still showing up.
I got it by separating out the $_.Extension -ne ".lnk" -and $_.Extension -ne ".url" . Cumbersome, but worked. Thoughts on throwing extensions into an array that can the adjusted? so if in one report i want to take out .ini files as well, but the next day, i want them in, just edit the array? thoughts?
Just create an array normally. $var =@("vslue1","value2","value3"); Then evaluate it normally using -notin $var
Added an exclusions array. $exclusions = @(".lnk",".url") then evaluated in the code as $_.extension -ne $exclusions but it still includes these extensions in the output. Am I not evaluating that correctly?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.