Free up HD space

I trying to clean up HD space
Script working , but this is what i cant get
i need in log file to see all file names from all directories that was deleted
I cant get it
This is code

## Computer CleanUp
function ComputerCleanup {
$computername = $env:COMPUTERNAME
$path = "C:\temp"
If(!(test-path $path))
      New-Item -ItemType Directory -Path $path -Force
      New-Item -ItemType Directory -Path $path -Force:$true | Out-Null

$data = Get-Date -Format 'dd-MM-yyyy'

#$log = "$path\$computername_user_cleanup_$data.log"
$log = "$path\"+"$computername"+"_"+"computer_cleanup"+"_"+"$data.log"
$Laptop = Get-CimInstance Win32_ComputerSystem | Where-Object PCSystemType -eq 2
$Laptop | Format-Table -Wrap -AutoSize| tee-object -filepath "$log"
##Display free drive space
$freespace_before = (([wmi]"root\cimv2:Win32_logicalDisk.DeviceID='C:'").FreeSpace/1GB).ToString("N2")+"GB"
$freespace_before | Out-File $log -Append
$dirs = @(
    "C:\Users\*\AppData\Local\Microsoft\Windows\Temporary Internet Files",
function Remove-OldWindowsUpdates {
#    #Delete the C:\Windows\SoftwareDistribution folder
    Get-Service wuauserv | Stop-Service -Force
    Start-Sleep -Seconds 3
    Get-ChildItem "$Env:windir\SoftwareDistribution" | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue -Verbose
    Start-Sleep -Seconds 3
if (Test-Path "$Env:windir\SoftwareDistribution") {
    Write-Warning "[-] Couldn't delete $Env:windir\SoftwareDistribution."} else {
    Write-Verbose "[+] Successfully deleted $Env:windir\SoftwareDistribution" -Verbose
    Get-Service wuauserv | Start-Service


$dirs | foreach {
    "-" * 60
    "Items deleted from directory $_"
    Get-ChildItem $_ -OutVariable df | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue | Out-Null
    "" * 2
} | out-file $log -Append
$freespace_after = (([wmi]"root\cimv2:Win32_logicalDisk.DeviceID='C:'").FreeSpace/1GB).ToString("N2")+"GB"
$freespace_after | Out-File $log -Append



welcome back.

Could you please edit your question and correct the code you posted? It’s all commented out.

Thanks in advacne.

1 Like

Your question is similar to this post that was just answered.

Move-item 2 issues - PowerShell Help - PowerShell Forums

The code currently is trying to collect the names of files in a directory, not what was actually deleted in a directory. If you want verbose logging, you need to process the files separately. Get-ChildItem is returning Files and Directories, so the code is deleting all subdirectories too which you are not collecting with Get-ChildItem. Another thing is just logging the name is a bit pointless, you at least want a path and name (e.g FullName), but even more you should be logging with a PSObject, not just text in a log file. Take look at the following (not tested, but should be close):

$dirs = "C:\Scripts\*"

$results = foreach ($dir in $dirs) {

    #Get all files in the directory and all sub-directories
    $files = Get-ChildItem -Path $dir -File -Recurse
    #Process the files one by one and delete them.
    foreach ($file in $files) {
        try {
            Remove-Item -Path $file.FullName -Force -ErrorAction Stop -WhatIf
            $Status = 'SUCCESS'
        catch {
            $Status = 'FAILED: {0}' -f $_

        #Return the file information to the $results variable
        $file | 
            Select-Object Name, 

    #Delete folders with no logging
    $folders = Get-ChildItem -Path $dir -Directory | 
                   Remove-Item -Force -Recurse -ErrorAction SilentlyContinue


$results | Where-Object {$_.Status -eq 'Success'}
$results | Where-Object {$_.Status -ne 'Success'}
#$results | Export-Csv -Path 'C:\mystuff.csv' -NoTypeInformation

It also appears that you are trying to write to the host with output. Rather than outputting a bunch of filenames to the screen, look at Write-Progress that will tell you how far in deleting process has been completed as you’ll just be scrolling with a bunch of names that doesn’t really provide any value and the buffer isn’t big enough to hold all of that data. Using a progress bar will be much more useful to who is monitoring the process.


Really? All files from all folders? Why? :face_with_monocle:

You can run “Remove-Item” with the parameter “-Verbose” and capture the verbose output with “4>&1” and pipe it to “Out-File” if you really want. But that sounds quite over the top to me. :wink:

Hi Rob,
Thank you for this example
look like this is what i can work with

Thank you Olaf for good suggestions,
i think Rob example, this is what can help me
i want this script will run on user machine on background to clean up HD space

You should have in mind that if you run this script in the background you may delete files that are still needed or in use by the currently logged on user or other users of the computer.
Why do you think you need to delete all these files/folders?

Hi Olaf, you maybe right and i need to think about all this folders, but most of them can be deleted with out any problem for user or cant harm system
If you have list of folders that i can remove without any issue i will be appreciated

Usually I don’t think about such a topic at all. We do not have computers where this would be necessary. :wink:

:slight_smile: Ohhh, lucky you
Thx, Olaf for all your help