diff --git a/Microsoft/Delete_temporary_files.ps1 b/Microsoft/Delete_temporary_files.ps1 new file mode 100644 index 0000000..a973c17 --- /dev/null +++ b/Microsoft/Delete_temporary_files.ps1 @@ -0,0 +1,76 @@ +# Script by Timur@0x01337.com +# Date: 2023-11-24 +<# +.DESCRIPTION +Deletes all files in the specified temporary folder. If no arguments are specified, deletes the files in the folder specified in the "TEMP" environment variable. + +Requires administrator privileges. + +.PARAMETER path +Optional. A path to folder with temporary files. +By default, uses the path specified in the 'TEMP' environment variable. + +.PARAMETER help +Displays a detailed usage description of this script. + +.EXAMPLE +PS> .\Delete-Temporary-Files.ps1 + +.EXAMPLE +PS> .\Delete-Temporary-Files.ps1 -path "path-to-tmp" + +.EXAMPLE +PS> .\Delete-Temporary-Files.ps1 -help +#> + +# Getting command line parameters +param ( + [parameter(Mandatory = $false)][string]$path, + [parameter(Mandatory = $false)][switch]$help +) + +$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) +$isAdmin = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) +if (!$isAdmin) { + Write-Error "This script requires administrator privileges" + exit 1 +} + +# Writing help message +if ($help) { + get-help $MyInvocation.MyCommand.Path -Full + exit 0 +} + +if (!$path) { + $path = $env:TEMP +} + +if (!(Test-Path $path)) { + Write-Error "$path is not exist" + exit 1 +} + +Write-Host "Temporary path to clean: $path" + +$items = Get-ChildItem -Path $path -Recurse +$beforeClean = $items.Count +Write-Host "$beforeClean files and folders to be cleaned" + +foreach ($item in $items) { + try { + Remove-Item -Path "$path\$item" -Force -Recurse -ErrorAction Stop + } + catch { + if ($_.tostring().Contains("it is being used by another process")) { + Write-Warning $_ + } + Continue + } +} + +$items = Get-ChildItem -Path $path -Recurse +$afterClean = $items.Count +Write-Host "$($beforeClean - $afterClean) files and folders was cleaned" + +exit 0