Powershell运行历史记录删除

时间:2012-05-18 16:35:21

标签: powershell registry

我正在尝试运行Powershell脚本以通过注册表清除“运行历史记录”。它工作得很好,但我遇到的问题是我希望它显示注册表值数据,但我无法正常显示它。这是脚本:

function Delete
{
$Reg = Get-RegistryValues 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU'
foreach ($Value in $Reg)
    {
    $Item = Get-ItemProperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU\' -name $Value
    $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes",""
    $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No",""
    $choices = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no)
    $caption = "Warning!"
    $message = ("Do you want to delete the run value "+$Item)
    $result = $Host.UI.PromptForChoice($caption,$message,$choices,0)
    if($result -eq 0) 
        {              
        Remove-ItemProperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU' -name $Value 
        }
    if($result -eq 1) { }
    }
}

function Get-RegistryValues($Key) 
{
(Get-Item $Key).GetValueNames()

}

Delete

每当我尝试运行它时,我得到$ Message

的以下输出
Do you want to delete the run value @{MRULIST=idhgfcaeb}

有没有人知道获取JUST价值数据的方法,所以它会是:

idhgfcaeb

工作解决方案:

function Delete
{
$Reg = Get-RegistryValues 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU'
foreach ($Value in $Reg)
    {
    if ($Value -eq 'MRUList') {Set-ItemProperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU\' -name $Value -value ' '}
    Else 
        {
        $Item = (Get-ItemProperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU\' -name $Value).$Value.TrimEnd("\1")
        $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes",""
        $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No",""
        $choices = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no)
        $caption = "Warning!"
        $message = ("Do you want to delete the run value "+$Item)
        $result = $Host.UI.PromptForChoice($caption,$message,$choices,0)
        if($result -eq 0) 
            {              
            Remove-ItemProperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU' -name $Value 
            }
        if($result -eq 1) { }
        }
    }
}

function Get-RegistryValues($Key) 
{
(Get-Item $Key).GetValueNames()
}

Delete

1 个答案:

答案 0 :(得分:3)

您可以使用:

$Item = (Get-ItemProperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU\' -Name mrulist).MRUList

或者:

("Do you want to delete the run value " + $Item.MRUList)