CMD Maintenance & Troubleshooting - Neilitlib/MSP-Simple-Commands GitHub Wiki

Delete Temp Files

PS

Remove-Item -path 'C:\Windows\Prefetch\*' -Recurse -Force -ErrorAction Continue -Verbose; Remove-Item -path 'C:\Windows\Temp\*' -Recurse -Force -ErrorAction Continue -Verbose; Remove-Item -path 'C:\Windows\Logs\CBS\*' -Recurse -Force -ErrorAction Continue -Verbose; Remove-Item -path 'C:\Users\*\AppData\Roaming\Microsoft\Windows\Recent Items\*' -Recurse -Force -ErrorAction Continue -Verbose; Get-WmiObject Win32_UserProfile | Where-Object { $_.Special -eq $false } | ForEach-Object { $recycleBinPath = Join-Path -Path $_.LocalPath -ChildPath '$Recycle.bin'; Get-ChildItem $recycleBinPath -Force | Remove-Item -Recurse -Force }; dism.exe /online /cleanup-image /startcomponentcleanup

Overall, the script aims to free up disk space by removing temporary files, logs, and unused user profile items. It's commonly used for system maintenance and optimization.

  1. Remove-Item Commands:

    • Remove-Item -path 'C:\Windows\Prefetch\*' -Recurse -Force -ErrorAction Continue -Verbose: Deletes all files in the 'C:\Windows\Prefetch' directory. The -Recurse flag ensures that subdirectories are also removed. -Force suppresses confirmation prompts, and -ErrorAction Continue allows the script to continue even if an error occurs.
    • Remove-Item -path 'C:\Windows\Temp\*' -Recurse -Force -ErrorAction Continue -Verbose: Deletes all files in the 'C:\Windows\Temp' directory.
    • Remove-Item -path 'C:\Windows\Logs\CBS\*' -Recurse -Force -ErrorAction Continue -Verbose: Deletes all files in the 'C:\Windows\Logs\CBS' directory.
    • Remove-Item -path 'C:\Users\*\AppData\Roaming\Microsoft\Windows\Recent Items\*' -Recurse -Force -ErrorAction Continue -Verbose: Deletes all files in the 'Recent Items' folder for all user profiles.
  2. User Profile Cleanup:

    • Get-WmiObject Win32_UserProfile | Where-Object { $_.Special -eq $false } | ForEach-Object { $recycleBinPath = Join-Path -Path $_.LocalPath -ChildPath '$Recycle.bin'; Get-ChildItem $recycleBinPath -Force | Remove-Item -Recurse -Force }: Iterates through all user profiles (excluding special profiles like Default and System) and empties their Recycle Bin by deleting files in the '$Recycle.bin' folder.
  3. Component Cleanup:

    • dism.exe /online /cleanup-image /startcomponentcleanup: Initiates the built-in Deployment Image Servicing and Management (DISM) tool to clean up the Windows component store. This helps reduce the size of the WinSxS folder.

Reset Network Stack

CLI

  1. ipconfig /release && ipconfig /flushdns && ipconfig /renew && netsh int ip reset
  • This will disconnect you but the use of && means you can type multiple commands including the renewal command to restore the connection.
  1. netsh winsock reset

Event Viewer Logs

  • You can see the REPEATABLE and EDITABLE table formats and object modifications which determine your output

PS

  • Can edit the number (50) but keep it to a minimum like 200 to 400
  • Max "supported" is roughly 2.1 million, but that can crash your device

- List All Recent Errors

Get-EventLog -LogName System -Newest 50 -EntryType Error | Format-Table -wrap

Displays 50 most recent "Error" events

image

- List Network Related Errors

Get-EventLog -LogName System -Newest 50 -EntryType Error | Where-Object { $_.Message -like '*network*' } | Format-Table -wrap

image

List Installed Software

PS

- Installed MSI/EXE

Get-WmiObject -Class Win32_Product | Select-Object Name, Version, InstallLocation | Sort-Object Name | Format-Table -wrap

Get-WmiObject -Class Win32_Product | Sort-Object Name | Format-Table -wrap

  • NOTE: may take some time to display results

image image

- Installed Apps/Store-Ware

PS

'Get-AppxPackage | Sort-Object -Property Name | Format-Table -Wrap'

image