Novice - Powershell exclude 1 subfolder and all of its folders / files within in

kaijie tan 1 Reputation point
2022-10-06T03:50:57.087+00:00

Hi i am trying to run some scripts to flush out the files that is 7 years or more and getting the file size, so i managed to put up with some script online. for now, i want to exclude 1 sub directory and all of its contents be it folder or files within that directory.

$Path = "E:\SharedFolder\Security\HSSE"
$datetime = (Get-Date).AddYears(-7)
Get-ChildItem -file -path $Path -force -Recurse -Exclude "HSSE Website" | Where-Object {$_.LastWriteTime -lt $datetime} | export-csv C:\temp\testexclude.csv -NoTypeInformation

can give me some guidance.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Limitless Technology 39,931 Reputation points
    2022-10-07T14:34:01.853+00:00

    Hello there,

    I am not sure what guidance you are in need, is the above script not working?

    The following script will delete items in my Downloads directory that are older than 6 months:

    $Folder = "G:\Downloads"

    Delete files older than 6 months

    Get-ChildItem $Folder -Recurse -Force -ea 0 |
    ? {!$.PsIsContainer -and $.LastWriteTime -lt (Get-Date).AddDays(-180)} |
    ForEach-Object {
    $_ | del -Force
    $_.FullName | Out-File C:\log\deletedlog.txt -Append
    }

    Delete empty folders and subfolders

    Get-ChildItem $Folder -Recurse -Force -ea 0 |
    ? {$.PsIsContainer -eq $True} |
    ? {$
    .getfiles().count -eq 0} |
    ForEach-Object {
    $_ | del -Force
    $_.FullName | Out-File C:\log\deletedlog.txt -Append
    }

    PowerShell Basics: How to Delete Files Older Than X Days https://techcommunity.microsoft.com/t5/itops-talk-blog/powershell-basics-how-to-delete-files-older-than-x-days/ba-p/1255317


    --If the reply is helpful, please Upvote and Accept it as an answer–

    0 comments No comments

  2. kaijie tan 1 Reputation point
    2022-10-10T00:48:34.77+00:00

    Hi thanks for the reply.

    the above scripts somehow, couldnt exclude that "particular folder" from the output.

    So what i actually needed is the gather the information of the files that is 7 years or old, include the size, to see that how much disc space it will occupying before any deletion, quite a lot of online resources is pointing to direct delete, which i do not want to. All i wish is to only search and list the files that is 7 years old or more and list of the individual file size.

    Thanks in advance.

    0 comments No comments

  3. MotoX80 36,401 Reputation points
    2022-10-10T13:52:27.183+00:00

    Something like this....

    $Path = "c:\program files"  
    $datetime = (Get-Date).AddYears(-7)  
    $files = Get-ChildItem -file -path $Path -force -Recurse -ErrorAction SilentlyContinue |   
        Where-Object {$_.LastWriteTime -lt $datetime} |   
        Where-Object {$_.DirectoryName -notmatch "Office16"}    
    $TotalSize = 0   
    foreach ($f in $files) {  
        $TotalSize += $f.Length  
        [PSCustomObject]@{  
            Size = $f.Length  
            LastModified = $f.LastWriteTime  
            Name = $f.FullName  
        }  
    }  
    ""  
    "Totalsize is {0}" -f $TotalSize      
    "File count = {0}" -f $files.count  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.