none
PowerShell で PC の掃除をする方法について RRS feed

  • 質問

  • 以下を PowerShell で実現できないか、と方法を探しています。

    • 「タスク バーと [スタート] メニューのプロパティ」 ⇒ 「[スタート] メニュー」タブ ⇒ 「プライバシー」 ※プログラムと項目の履歴を消したい
    • 「インターネットのプロパティ」 (インターネット オプション) ⇒ 「詳細設定」タブ ⇒ 「リセット」
    • 「システムのプロパティ」 ⇒ 「システムの保護」タグ ⇒ 「(システム ドライブ)」の「構成」 ⇒ 「削除」

    ウィンドウを PowerShell から開くか、GUI 操作以外で目的を達成する方法を探しています。

    ウィンドウを開く場合、贅沢を言えば、表示するタブも指定できると有り難いです。

    復元ポイントを新規作成する場合は、Checkpoint-Computer で行けそうなんですが、消す方法が見つけられませんでした。

    プログラムと項目の履歴はレジストリを見つけて直接いじるのも手か、と思っています。

    プラウザ設定のリセットは、ウィンドウは inetcpl.cpl で開けることは判っているのですが、rundll32 を使って直接リセットする方法があれば知りたいです。

    何かよい方法を思いつく方はいらっしゃいますか?

    ※誰かの参考になるかもなので、今作ってるスクリプトを以下に記載します。

    # ごみ箱を空にする
    $shell = New-Object -comObject Shell.Application
    $recycle_bin = $shell.NameSpace(10)
    foreach($item in $recycle_bin.Items()){
        Remove-Item -Force -Recurse $item.Path
    }
    explorer shell:RecycleBinFolder
    
    
    # 一時フォルダおよび WER ファイルの削除
    Remove-Item -Force -Recurse C:\Windows\Temp\* 2> Out-Null
    Remove-Item -Force -Recurse C:\ProgramData\Microsoft\Windows\WER\ReportArchive\* 2> Out-Null
    Remove-Item -Force -Recurse C:\ProgramData\Microsoft\Windows\WER\ReportQueue\* 2> Out-Null
    $profiles = gci C:\Users
    foreach ($profile in $profiles) {
        Remove-Item -Force -Recurse ($profile.FullName + "\AppData\Local\Temp\*") 2> Out-Null
        Remove-Item -Force -Recurse ($profile.FullName + "\AppData\Local\Microsoft\Windows\WER\ReportArchive\*") 2> Out-Null
        Remove-Item -Force -Recurse ($profile.FullName + "\AppData\Local\Microsoft\Windows\WER\ReportQueue\*") 2> Out-Null
    }
    
    # イベント ログの消去
    $evntlogs = (wevtutil enum-logs)
    
    foreach ($evntlog in $evntlogs) {
        wevtutil clear-log $evntlog 2> Out-Null
    }
    wevtutil clear-log Security


    2016年1月20日 2:52

回答

  • こんな感じでいけると思います。

    #最近開いたプログラム/項目の削除
    Remove-Item HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\UserAssist -Recurse
    Stop-Process -Name explorer -Force
    
    #インターネットオプションリセット
    add-type -AssemblyName System.Windows.Forms
    rundll32 InetCpl.cpl,ResetIEtoDefaults
    Start-Sleep 1
    [System.Windows.Forms.SendKeys]::SendWait("%(r)")
    
    #復元ポイント削除
    vssadmin delete shadows /all /quiet


    • 編集済み LazyDogg 2016年1月20日 8:06 コマンドの間違いを修正
    • 回答としてマーク DJ_Kaosun 2016年1月21日 17:32
    2016年1月20日 3:54

すべての返信

  • 記載し忘れましたが、システムのプロパティは sysdm.cpl ですね。
    2016年1月20日 2:58
  • こんな感じでいけると思います。

    #最近開いたプログラム/項目の削除
    Remove-Item HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\UserAssist -Recurse
    Stop-Process -Name explorer -Force
    
    #インターネットオプションリセット
    add-type -AssemblyName System.Windows.Forms
    rundll32 InetCpl.cpl,ResetIEtoDefaults
    Start-Sleep 1
    [System.Windows.Forms.SendKeys]::SendWait("%(r)")
    
    #復元ポイント削除
    vssadmin delete shadows /all /quiet


    • 編集済み LazyDogg 2016年1月20日 8:06 コマンドの間違いを修正
    • 回答としてマーク DJ_Kaosun 2016年1月21日 17:32
    2016年1月20日 3:54
  • ありがとうございます。無事行けました。

    SendKeys で操作したいウィンドウがアクティブにならない時がありますが、大した問題ではないので、これで完成とします。

    Write-Output "■処理を開始します。"
    Add-Type -AssemblyName System.Windows.Forms
    
    Write-Output "■ごみ箱を空にします。"
    $shell = New-Object -comObject Shell.Application
    $recycle_bin = $shell.NameSpace(10)
    foreach($item in $recycle_bin.Items()){
        Remove-Item -Force -Recurse $item.Path
    }
    explorer shell:RecycleBinFolder
    Start-Sleep 2
    [System.Windows.Forms.SendKeys]::SendWait("%{F4}")
    
    
    Write-Output "■一時フォルダ, WER ファイル, 最近開いたプログラム/項目/場所を削除します。"
    Stop-Service "Windows Update"
    Stop-Service "Background Intelligent Transfer Service"
    Remove-Item -Force -Recurse C:\Windows\SoftwareDistribution\*
    Remove-Item -Force -Recurse C:\Windows\Temp\* 2> $null
    Remove-Item -Force -Recurse C:\ProgramData\Microsoft\Windows\WER\ReportArchive\* 2> $null
    Remove-Item -Force -Recurse C:\ProgramData\Microsoft\Windows\WER\ReportQueue\* 2> $null
    $profiles = gci C:\Users
    foreach ($profile in $profiles) {
        Remove-Item -Force -Recurse ($profile.FullName + "\AppData\Local\Temp\*") 2> $null
        Remove-Item -Force -Recurse ($profile.FullName + "\AppData\Local\Microsoft\Windows\WER\ReportArchive\*") 2> $null
        Remove-Item -Force -Recurse ($profile.FullName + "\AppData\Local\Microsoft\Windows\WER\ReportQueue\*") 2> $null
        Remove-Item -Force -Recurse ($profile.FullName + "\AppData\Roaming\Microsoft\Windows\Recent\*") 2> $null
    }
    
    Write-Output "■イベント ログを消去します。"
    $evntlogs = (wevtutil enum-logs)
    
    foreach ($evntlog in $evntlogs) {
        Write-Output ("  ログ消去: " + $evntlog)
        wevtutil clear-log $evntlog > $null
    }
    wevtutil clear-log Security
    
    Write-Output "■復元ポイントをすべて削除し、現在の状態の復元ポイントを作成します。"
    Write-Output "復元ポイントをすべて削除します。"
    vssadmin delete shadows /all /quiet
    Write-Output "復元ポイントを作成します。"
    Checkpoint-Computer -Description "Initial Checkpoint"
    Get-ComputerRestorePoint
    
    Write-Output "■インターネット オプションの設定をリセットします。"
    rundll32 InetCpl.cpl,ResetIEtoDefaults
    Start-Sleep 2
    [System.Windows.Forms.SendKeys]::SendWait("%(p)")
    [System.Windows.Forms.SendKeys]::SendWait("%(r)")
    New-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings" -Name AutoConfigURL -PropertyType String -Value "" -Force > $null
    Remove-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings" -Name AutoConfigURL
    New-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings" -Name ProxyServer -PropertyType String -Value "" -Force > $null
    Remove-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings" -Name ProxyServer
    New-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings" -Name ProxyOverride -PropertyType String -Value "<local>" -Force > $null
    
    # pause
    Read-Host "■このウィンドウを閉じるには Enter キーを押して下さい。"



    2016年1月21日 17:36