首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用PowerShell清理磁盘

使用PowerShell清理磁盘
EN

Stack Overflow用户
提问于 2016-09-18 23:53:45
回答 2查看 4.1K关注 0票数 1

我正在试着写一个清理磁盘的脚本。除了运行cleanmgr之外,所有程序都可以工作。有人能给点建议吗?

代码语言:javascript
运行
复制
$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
}
EN

回答 2

Stack Overflow用户

发布于 2019-09-26 02:59:37

2#正在运行磁盘清理工具

代码语言:javascript
运行
复制
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     
票数 0
EN

Stack Overflow用户

发布于 2019-09-26 06:40:12

我知道这个问题有点老生常谈了,但是对于那些还在寻找的人来说...

此链接可以提供两个options,如下图所示

代码语言:javascript
运行
复制
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}}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39559604

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档