Skip to content

Instantly share code, notes, and snippets.

@paul-d-ray
Last active February 12, 2026 11:21
Show Gist options
  • Select an option

  • Save paul-d-ray/677d52e3fdccb804f908c1307f63a5a1 to your computer and use it in GitHub Desktop.

Select an option

Save paul-d-ray/677d52e3fdccb804f908c1307f63a5a1 to your computer and use it in GitHub Desktop.
Nushell Cleanup Temp Folder

Cleanup temp files in the Temp folder

I have been using a cmd file to do this but wrote this using Nushell.

Nushell Code

#!/usr/bin/env nu

# PR 20250503_1747

# PR 20250913_0020
# Clean up specific files and directories in the TEMP folder

# PR 2026-02-11 22:28:36
# Added the Remote Proxy directories to be removed.

# PR 2026-02-11 22:33:24
# Added the Wezterm Blob directoires to be removed.

#**********************************************************************

# Define reusable function to clean items
def clean-temp-items [
    name: string,                    # Description of what is being removed
    wfilter: closure                 # Closure to filter items
] {

    let items = (nuls $env.TEMP -f | where $wfilter)
    let count = ($items | length)
    print $"Trying to remove ($count) ($name) from the ($env.TEMP) folder."
    #print $items.fullname
    $items | each {|item| try { rm -p -f $item.fullname } catch { } }
}

# Clean temp files with specific extensions
let ExtList = '(evtx|log|tmp|txt)'

let Beforecleanup = nuls $env.TEMP | where type == file and ext =~ $ExtList |
  group-by ext --to-table | aggregate


clean-temp-items 'evtx files' {|it| $it.type == 'file' and $it.ext =~ '(evtx)'}
clean-temp-items 'log files'  {|it| $it.type == 'file' and $it.ext =~ '(log)'}
clean-temp-items 'tmp files'  {|it| $it.type == 'file' and $it.ext =~ '(tmp)'}
clean-temp-items 'txt files'  {|it| $it.type == 'file' and $it.ext =~ '(txt)'}

# Clean Chrome directories
clean-temp-items 'Chrome directories' {|it| $it.type == 'dir' and $it.name =~ 'chrome' }

# Clean TMP directories
clean-temp-items 'TMP directories' {|it| $it.type == 'dir' and $it.name =~ '\.tmp' }

# Clean AppxErrorReport files
clean-temp-items 'AppxErrorReport files' {|it| $it.type == 'file' and $it.name =~ '^appxerrorreport' }

# Remote Proxy directories
clean-temp-items 'Remote Proxy directories' {|it| $it.type == 'dir' and $it.name =~ 'remoteipmoproxy_configdefender' }

# wezterm-blob-lease directories
clean-temp-items 'Wezterm blob directories' {|it| $it.type == 'dir' and $it.name =~ 'wezterm-blob-lease' }

#########
# report before and after cleanup
let Aftercleanup = nuls $env.TEMP | where type == file and ext =~ $ExtList |
  group-by ext --to-table | aggregate 

print "\n"

##print 'Before and After Cleanup'
##($Beforecleanup | rename ext before) | 
##  join --outer ($Aftercleanup | rename ext after) ext | 
##  insert difference { |row| $row.before - ($row.after | 
##    default 0) }

($Beforecleanup | rename ext before)
  | join --outer ($Aftercleanup | rename ext after) ext
  | update after { |row| $row.after | default 0 }
  | insert difference { |row| $row.before - $row.after }

Ouput

Trying to remove 0 evtx files from the d:\temp folder.
Trying to remove 1 log files from the d:\temp folder.
Trying to remove 55 tmp files from the d:\temp folder.
Trying to remove 2 txt files from the d:\temp folder.
Trying to remove 0 Chrome directories from the d:\temp folder.
Trying to remove 4 TMP directories from the d:\temp folder.
Trying to remove 0 AppxErrorReport files from the d:\temp folder.
Trying to remove 0 Remote Proxy directories from the d:\temp folder.
Trying to remove 0 Wezterm blob directories from the d:\temp folder.


╭───┬─────┬────────┬───────┬────────────╮
│ # │ ext │ before │ after │ difference │
├───┼─────┼────────┼───────┼────────────┤
│ 0 │ tmp │     55 │    55 │          0 │
│ 1 │ txt │      2 │     2 │          0 │
│ 2 │ log │      1 │     1 │          0 │
╰───┴─────┴────────┴───────┴────────────╯
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment