Files
management-scripts/PSModule/PSModule-Cleanup.psm1
T
2020-08-14 14:19:37 -04:00

63 lines
2.2 KiB
PowerShell

Function global:Cleanup-SystemPath
{
## Get contents of SYSTEM PATH environment variable
$REG_ENVVAR = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
$CURRENT_PATH = (Get-Itemproperty -Path $REG_ENVVAR -Name Path).Path
$NEW_PATH = $null
$REMOVE_PATH =$null
## Verify each path
Foreach ( $path in $CURRENT_PATH.Split(";") )
{
If ( (Test-Path $path) -or ($path -like "*%*%*") )
{
If ( $NEW_PATH ) { $NEW_PATH += ";${path}" }
Else { $NEW_PATH = $path }
}
Else
{
If ( $REMOVE_PATH ) { $REMOVE_PATH += ";${path}" }
Else { $REMOVE_PATH = $path }
}
}
Write-Output "Removed Paths:`n`r${REMOVE_PATH}"
Set-ItemProperty -Path $REG_ENVVAR -Name Path -Value $NEW_PATH
}
## This script will remove runaway AppX temp files that can consume 100's of GB on an endpoint
Function global:Cleanup-AppXTempFiles
{
## Only run this script on Windows 8 or higher endpoints
$WindowsVersion = [double]("{0}.{1}" -f ([System.Environment]::OSVersion.Version).Major,([System.Environment]::OSVersion.Version).Minor)
If ( $WindowsVersion -ge 6.2 )
{
Get-ChildItem -Path "${Env:SystemRoot}\Temp" | Where { $_.Name -like "AppXDeploymentServer_*.evtx" }| Remove-Item -Force
Get-ChildItem -Path "${Env:SystemRoot}\Temp" | Where { $_.Name -like "AppxErrorReport_*.txt" } | Remove-Item -Force
Get-ChildItem -Path "${Env:SystemRoot}\Temp" | Where { $_.Name -like "AppXPackaging_*.evtx" }| Remove-Item -Force
}
}
## This script will delete runaway CBS logs that can take up 100's of GB on an endpoint
Function global:Cleanup-LogFiles
{
Get-ChildItem -Path "${Env:WINDIR}\Logs\CBS" -File | Where { ( $_.Name -like "CbsPersist_*.log" ) -or ( $_.Name -like "CbsPersist_*.cab" ) } | Remove-Item -Force
}
Function global:Cleanup-TempFiles
{
#[CmdletBinding()]
param([int]$OlderThan=7)
## Create a new timespan to compare with last write date of the target directory
$timedelta = New-TimeSpan -Days $OlderThan
## Remove all files and directories
Foreach ( $item in (Get-ChildItem -Path "${Env:WinDir}\TEMP") )
{
## If it's older than the number of days specified, recursively delete the directory
If ( $item.LastWriteTime -lt ((Get-Date) - $timedelta) ) { Remove-Item $item.FullName -Recurse -ErrorAction SilentlyContinue -Force }
}
}