使用PowerShell进行磁盘清理

时间:2016-09-18 15:53:45

标签: powershell

我正在尝试编写用于清理磁盘的脚本。除了运行cleanmgr之外的所有工作。有人可以建议吗?

$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
}

2 个答案:

答案 0 :(得分:0)

2#运行磁盘清理工具

write-Host "Finally now , Running Windows disk Clean up Tool" -ForegroundColor Cyan
cleanmgr /sagerun:1 /VERYLOWDISK | out-Null 

$([char]7)
Sleep 1 
$([char]7)
Sleep 1     

答案 1 :(得分:0)

我知道这个问题很旧,但是对于仍在寻找的人...

此链接可以提供一对options,就像下面的

foreach ($computer in $computers){
if(!(Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
{write-host "cannot reach $computer" -f red}

else {& \\$computer\C$\Windows\System32\cleanmgr.exe /sagerun:1}}