title: Disk cleanup using PowerShell
body: <p>I am trying to write a script for cleaning disks. All works except running <code>cleanmgr</code>. Can someone advise?</p>
  
  <pre><code>$objShell = New-Object -ComObject Shell.Application
  $objFolder = $objShell.Namespace(0xA)
  $computername = Read-Host -Prompt 'Enter machine name'
  
  foreach ($hosts in $computername) {
    #$temp = get-ChildItem "env:\TEMP"
    #$temp2 = $emp.Value
    #$swtools = "c:\SWTOOLS\*"
    $ccmcache = "\\$hosts\c$\Windows\ccmcache\*"
    $temp = "\\$hosts\C$\Users\*****\AppData\Local\Temp\*"
    #$WinTemp = "c:\Windows\Temp\*"
  
    $FreespaceBefore = (Get-WmiObject -Class win32_logicaldisk -ComputerName $hosts -Filter "DeviceID='C:'" | select Freespace).FreeSpace/1GB
  
    #Write-Output "Disk Space Before"
    Write-Output "$FreespaceBefore"
  
    Write-Host "Removing Junk files in $ccmcache on $hosts." -ForegroundColor Green
    Remove-Item -Recurse $ccmcache -Force -Verbose
  
    Write-Host "Removing Junk files in $temp on $hosts." -ForegroundColor DarkYellow
    Remove-Item -Recurse $temp -Force -Verbose
  
    Write-Host "Finally now , Running Windows disk Clean up Tool" -ForegroundColor Cyan
  
    Invoke-Command -ComputerName pc -ScriptBlock {Start-Process cleanmgr.exe}
  
    #Write-Output "Disk Space after"
    #Write-Output "$diskspaceafter"
  
    $([char]7)
    Sleep 1
    $([char]7)
    Sleep 1
  
    Write-Host "I finished the cleanup task,Succefully " -ForegroundColor Yellow
  }
  </code></pre>